8000 add unaligned memory access flags by can-lehmann · Pull Request #162 · can-lehmann/metajit.cpp · GitHub
[go: up one dir, main page]

Skip to content

add unaligned memory access flags - #162

Merged
can-lehmann merged 2 commits into
mainfrom
can-unaligned-memory
Nov 18, 2026
Merged

can-lehmann merged 2 commits into
mainfrom
can-unaligned-memory

Conversation

@can-lehmann
Copy link
Copy Markdown
Owner

No description provided.

@github-actions
Copy link
Copy Markdown

Coverage

File Lines Functions Branches
bitprovenance.hpp 🟢 100.00% 🟢 100.00% 🟡 81.90%
codeheap.hpp 🔴 65.38% 🟢 100.00% 🔴 62.50%
genext.hpp 🟡 73.94% 🟡 87.50% 🟡 75.99%
jitir.hpp 🟡 72.82% 🟡 79.62% 🟡 73.13%
jitir_llvmapi.hpp 🔴 54.65% 🟡 80.00% 🔴 50.00%
llvmgen.hpp 🟢 90.38% 🟢 90.91% 🟡 79.66%
lowerllvm.hpp 🟡 70.72% 🟡 87.50% 🔴 51.42%
x86gen.hpp 🟡 88.84% 🟡 85.19% 🟡 83.17%
x86insts.inc.hpp 🟡 80.87% - -
TOTAL 🟡 75.99% 🟡 81.57% 🟡 77.31%
Diff Coverage

Diff Coverage

Diff: origin/main...HEAD, staged and unstaged changes

  • genext.hpp (100%)
  • jitir.hpp (62.7%): Missing lines 1769,1778-1779,1797-1799,1815,1826,4249-4250,5386-5390,6838-6839,6849-6850
  • jitir_llvmapi.hpp (33.3%): Missing lines 706,709
  • llvmgen.hpp (60.0%): Missing lines 252-253,264-265
  • lowerllvm.hpp (60.0%): Missing lines 197-198,205-206

Summary

  • Total: 77 lines
  • Missing: 29 lines
  • Coverage: 62%

jitir.hpp

Lines 1765-1773

  1765   Value* value() const { return arg(1); }
  1766   StoreFlags flags() const { return _flags; }
  1767   AliasingGroup aliasing() const { return _aliasing; }
  1768   uint64_t offset() const { return _offset; }
! 1769   void set_flags(StoreFlags flags) { _flags = flags; }
  1770   void set_aliasing(AliasingGroup aliasing) { _aliasing = aliasing; }
  1771   void set_offset(uint64_t offset) { _offset = offset; }
  1772   void write(metajit::PrettyStream& stream) const override {
  1773     stream << Highlight::Keyword << "Store" << Highlight::None;

Lines 1774-1783

  1774     stream << ' ';
  1775     bool is_first = true;
  1776     write_args(stream, is_first);
  1777     if ((uint32_t) _flags != StoreFlags::None) {
! 1778       stream << ", "; stream << Highlight::ArgName << "flags=" << Highlight::None; stream << _flags;
! 1779     }
  1780     stream << ", "; stream << Highlight::ArgName << "aliasing=" << Highlight::None; stream << Highlight::Constant << _aliasing << Highlight::None;
  1781     stream << ", "; stream << Highlight::ArgName << "offset=" << Highlight::None; stream << Highlight::Constant << _offset << Highlight::None;
  1782   }
  1783   void write_json(std::ostream& stream) const override {

Lines 1793-1803

  1793     stream << ", ";
  1794     stream << "\"type\": ";
  1795     stream << "\"" << type() << "\"";
  1796     stream << ", ";
! 1797     stream << "\"flags\": ";
! 1798     _flags.write_json(stream);
! 1799     stream << ", ";
  1800     stream << "\"aliasing\": ";
  1801     stream << _aliasing;
  1802     stream << ", ";
  1803     stream << "\"offset\": ";

Lines 1811-1819

  1811     if (arg_count() != other_inst->arg_count()) { return false; }
  1812     for (size_t it = 0; it < arg_count(); it++) {
  1813       if (arg(it) != other_inst->arg(it)) { return false; }
  1814     }
! 1815     if (_flags != other_inst->_flags) { return false; }
  1816     if (_aliasing != other_inst->_aliasing) { return false; }
  1817     if (_offset != other_inst->_offset) { return false; }
  1818     return true;
  1819   }

Lines 1822-1830

  1822     hash ^= std::hash<size_t>()(arg_count());
  1823     for (size_t it = 0; it < arg_count(); it++) {
  1824       hash ^= std::hash<Value*>()(arg(it));
  1825     }
! 1826     hash ^= std::hash<StoreFlags>()(_flags);
  1827     hash ^= std::hash<AliasingGroup>()(_aliasing);
  1828     hash ^= std::hash<uint64_t>()(_offset);
  1829     return hash;
  1830   }

Lines 4245-4254

  4245       return build_load(ptr, type, flags, aliasing, offset);
  4246     }
  4247 
  4248     Value* fold_store(Value* ptr, Value* value, AliasingGroup aliasing, uint64_t offset) {
! 4249       return fold_store(ptr, value, StoreFlags::None, aliasing, offset);
! 4250     }
  4251 
  4252     Value* fold_store(Value* ptr, Value* value, StoreFlags flags, AliasingGroup aliasing, uint64_t offset) {
  4253       if (dynmatch(AddPtrInst, add_ptr, ptr)) {
  4254         if (dynmatch(Const, const_offset, add_ptr->offset())) {

Lines 5382-5394

  5382     Value* value = read_value_arg();
  5383     expect_char(','); skip_whitespace();
  5384     StoreFlags flags = StoreFlags::None;
  5385     if (_stream.peek() == 'f') {
! 5386       expect_word("flags");
! 5387       expect_char('=');
! 5388       flags = read_flags<StoreFlags>();
! 5389       expect_char(',');
! 5390     }
  5391     expect_word("aliasing");
  5392     expect_char('=');
  5393     AliasingGroup aliasing = read_int32();
  5394     expect_char(','); skip_whitespace();

Lines 6834-6843

  6834         assert(!is_poison);
  6835         size_t size = type_size(type);
  6836         assert(size > 0);
  6837         uint64_t bits = value;
! 6838         if constexpr (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) {
! 6839           bits <<= (sizeof(bits) - size) * 8;
  6840         }
  6841         std::memcpy(ptr, &bits, size);
  6842       }

Lines 6845-6854

  6845         uint64_t value = 0;
  6846         size_t size = type_size(type);
  6847         assert(size > 0);
  6848         std::memcpy(&value, ptr, size);
! 6849         if constexpr (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) {
! 6850           value >>= (sizeof(value) - size) * 8;
  6851         }
  6852         return Bits::constant(type, value);
  6853       }

jitir_llvmapi.hpp

Lines 702-713

  702     std::vector<llvm::Value*> build_args;
  703     build_args.push_back(jitir_builder);
  704     build_args.push_back(args[0]);
  705     build_args.push_back(args[1]);
! 706     build_args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), (uint64_t)i->flags(), false));
  707     build_args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), (uint64_t)i->aliasing(), false));
  708     build_args.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(context), (uint64_t)i->offset(), false));
! 709     assert(build_args.size() == 6);
  710     llvm::Value* built = builder.CreateCall(api.build_store, build_args);
  711     return built;
  712   }
  713   if (AllocaInst* i = dynamic_cast<AllocaInst*>(inst)) {

llvmgen.hpp

Lines 248-257

  248           emit_type(load->type()),
  249           emit_add_offset(emit_arg(load->arg(0)), load->offset())
  250         );
  251         if (load->flags().has(LoadFlags::Unaligned)) {
! 252           result->setAlignment(llvm::Align(1));
! 253         }
  254         if (load->flags().has(LoadFlags::Pure)) {
  255           result->setMetadata(llvm::LLVMContext::MD_invariant_load, llvm::MDNode::get(_context, {}));
  256         }
  257         return result;

Lines 260-269

  260           emit_arg(store->value()),
  261           emit_add_offset(emit_arg(store->ptr()), store->offset())
  262         );
  263         if (store->flags().has(StoreFlags::Unaligned)) {
! 264           result->setAlignment(llvm::Align(1));
! 265         }
  266         return result;
  267       } else if (dynmatch(AllocaInst, alloca, inst)) {
  268         llvm::AllocaInst* llvm_alloca;
  269         if (_alloca_types.find(alloca) != _alloca_types.end() && _alloca_types.at(alloca) != Type::Void) {

lowerllvm.hpp

Lines 193-202

  193         Value* ptr = lower_operand(load->getPointerOperand());
  194         Type type = lower_type(load->getType());
  195         LoadFlags flags = LoadFlags::None;
  196         if (load->getAlign().value() < type_size(type)) {
! 197           flags = LoadFlags::Unaligned;
! 198         }
  199         return _builder.fold_load(ptr, type, flags, AliasingGroup(0), 0);
  200       } else if (llvm::StoreInst* store = llvm::dyn_cast<llvm::StoreInst>(inst)) {
  201         Value* ptr = lower_operand(store->getPointerOperand());
  202         Value* value = lower_operand(store->getValueOperand());

Lines 201-210

  201         Value* ptr = lower_operand(store->getPointerOperand());
  202         Value* value = lower_operand(store->getValueOperand());
  203         StoreFlags flags = StoreFlags::None;
  204         if (store->getAlign().value() < type_size(value->type())) {
! 205           flags = StoreFlags::Unaligned;
! 206         }
  207         return _builder.fold_store(ptr, value, flags, AliasingGroup(0), 0);
  208       } else if (llvm::GetElementPtrInst* gep = llvm::dyn_cast<llvm::GetElementPtrInst>(inst)) {
  209         Value* ptr = lower_operand(gep->getPointerOperand());
  210         assert(ptr->type() == Type::Ptr);

@github-actions
Copy link
Copy Markdown

Coverage

File Lines Functions Branches
bitprovenance.hpp 🟢 100.00% 🟢 100.00% 🟡 82.13%
codeheap.hpp 🔴 65.38% 🟢 100.00% 🔴 62.50%
genext.hpp 🟡 73.94% 🟡 87.50% 🟡 75.99%
jitir.hpp 🟡 72.88% 🟡 79.94% 🟡 73.41%
jitir_llvmapi.hpp 🔴 54.65% 🟡 80.00% 🔴 50.00%
llvmgen.hpp 🟢 90.38% 🟢 90.91% 🟡 79.66%
lowerllvm.hpp 🟡 70.72% 🟡 87.50% 🔴 51.42%
x86gen.hpp 🟡 88.84% 🟡 85.19% 🟡 83.17%
x86insts.inc.hpp 🟡 80.87% - -
TOTAL 🟡 76.02% 🟡 81.80% 🟡 77.45%
Diff Coverage

Diff Coverage

Diff: origin/main...HEAD, staged and unstaged changes

  • genext.hpp (100%)
  • jitir.hpp (73.9%): Missing lines 1769,1798-1800,1816,1827,4250-4251,6826-6827,6837-6838
  • jitir_llvmapi.hpp (33.3%): Missing lines 706,709
  • llvmgen.hpp (60.0%): Missing lines 252-253,264-265
  • lowerllvm.hpp (60.0%): Missing lines 197-198,205-206

Summary

  • Total: 72 lines
  • Missing: 22 lines
  • Coverage: 69%

jitir.hpp

Lines 1765-1773

  1765   Value* value() const { return arg(1); }
  1766  
8000
 StoreFlags flags() const { return _flags; }
  1767   AliasingGroup aliasing() const { return _aliasing; }
  1768   uint64_t offset() const { return _offset; }
! 1769   void set_flags(StoreFlags flags) { _flags = flags; }
  1770   void set_aliasing(AliasingGroup aliasing) { _aliasing = aliasing; }
  1771   void set_offset(uint64_t offset) { _offset = offset; }
  1772   void write(metajit::PrettyStream& stream) const override {
  1773     stream << Highlight::Keyword << "Store" << Highlight::None;;

Lines 1794-1804

  1794     stream << ", ";
  1795     stream << "\"type\": ";
  1796     stream << "\"" << type() << "\"";
  1797     stream << ", ";
! 1798     stream << "\"flags\": ";
! 1799     _flags.write_json(stream);
! 1800     stream << ", ";
  1801     stream << "\"aliasing\": ";
  1802     stream << _aliasing;
  1803     stream << ", ";
  1804     stream << "\"offset\": ";

Lines 1812-1820

  1812     if (arg_count() != other_inst->arg_count()) { return false; }
  1813     for (size_t it = 0; it < arg_count(); it++) {
  1814       if (arg(it) != other_inst->arg(it)) { return false; }
  1815     }
! 1816     if (_flags != other_inst->_flags) { return false; }
  1817     if (_aliasing != other_inst->_aliasing) { return false; }
  1818     if (_offset != other_inst->_offset) { return false; }
  1819     return true;
  1820   }

Lines 1823-1831

  1823     hash ^= std::hash<size_t>()(arg_count());
  1824     for (size_t it = 0; it < arg_count(); it++) {
  1825       hash ^= std::hash<Value*>()(arg(it));
  1826     }
! 1827     hash ^= std::hash<StoreFlags>()(_flags);
  1828     hash ^= std::hash<AliasingGroup>()(_aliasing);
  1829     hash ^= std::hash<uint64_t>()(_offset);
  1830     return hash;
  1831   }

Lines 4246-4255

  4246       return build_load(ptr, type, flags, aliasing, offset);
  4247     }
  4248 
  4249     Value* fold_store(Value* ptr, Value* value, AliasingGroup aliasing, uint64_t offset) {
! 4250       return fold_store(ptr, value, StoreFlags::None, aliasing, offset);
! 4251     }
  4252 
  4253     Value* fold_store(Value* ptr, Value* value, StoreFlags flags, AliasingGroup aliasing, uint64_t offset) {
  4254       if (dynmatch(AddPtrInst, add_ptr, ptr)) {
  4255         if (dynmatch(Const, const_offset, add_ptr->offset())) {

Lines 6822-6831

  6822         assert(!is_poison);
  6823         size_t size = type_size(type);
  6824         assert(size > 0);
  6825         uint64_t bits = value;
! 6826         if constexpr (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) {
! 6827           bits <<= (sizeof(bits) - size) * 8;
  6828         }
  6829         std::memcpy(ptr, &bits, size);
  6830       }

Lines 6833-6842

  6833         uint64_t value = 0;
  6834         size_t size = type_size(type);
  6835         assert(size > 0);
  6836         std::memcpy(&value, ptr, size);
! 6837         if constexpr (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) {
! 6838           value >>= (sizeof(value) - size) * 8;
  6839         }
  6840         return Bits::constant(type, value);
  6841       }

jitir_llvmapi.hpp

Lines 702-713

  702     std::vector<llvm::Value*> build_args;
  703     build_args.push_back(jitir_builder);
  704     build_args.push_back(args[0]);
  705     build_args.push_back(args[1]);
! 706     build_args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), (uint64_t)i->flags(), false));
  707     build_args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), (uint64_t)i->aliasing(), false));
  708     build_args.push_back(llvm::ConstantInt::get(llvm::Type::getInt64Ty(context), (uint64_t)i->offset(), false));
! 709     assert(build_args.size() == 6);
  710     llvm::Value* built = builder.CreateCall(api.build_store, build_args);
  711     return built;
  712   }
  713   if (AllocaInst* i = dynamic_cast<AllocaInst*>(inst)) {

llvmgen.hpp

Lines 248-257

  248           emit_type(load->type()),
  249           emit_add_offset(emit_arg(load->arg(0)), load->offset())
  250         );
  251         if (load->flags().has(LoadFlags::Unaligned)) {
! 252           result->setAlignment(llvm::Align(1));
! 253         }
  254         if (load->flags().has(LoadFlags::Pure)) {
  255           result->setMetadata(llvm::LLVMContext::MD_invariant_load, llvm::MDNode::get(_context, {}));
  256         }
  257         return result;

Lines 260-269

  260           emit_arg(store->value()),
  261           emit_add_offset(emit_arg(store->ptr()), store->offset())
  262         );
  263         if (store->flags().has(StoreFlags::Unaligned)) {
! 264           result->setAlignment(llvm::Align(1));
! 265         }
  266         return result;
  267       } else if (dynmatch(AllocaInst, alloca, inst)) {
  268         llvm::AllocaInst* llvm_alloca;
  269         if (_alloca_types.find(alloca) != _alloca_types.end() && _alloca_types.at(alloca) != Type::Void) {

lowerllvm.hpp

Lines 193-202

  193         Value* ptr = lower_operand(load->getPointerOperand());
  194         Type type = lower_type(load->getType());
  195         LoadFlags flags = LoadFlags::None;
  196         if (load->getAlign().value() < type_size(type)) {
! 197           flags = LoadFlags::Unaligned;
! 198         }
  199         return _builder.fold_load(ptr, type, flags, AliasingGroup(0), 0);
  200       } else if (llvm::StoreInst* store = llvm::dyn_cast<llvm::StoreInst>(inst)) {
  201         Value* ptr = lower_operand(store->getPointerOperand());
  202         Value* value = lower_operand(store->getValueOperand());

Lines 201-210

  201         Value* ptr = lower_operand(store->getPointerOperand());
  202         Value* value = lower_operand(store->getValueOperand());
  203         StoreFlags flags = StoreFlags::None;
  204         if (store->getAlign().value() < type_size(value->type())) {
! 205           flags = StoreFlags::Unaligned;
! 206         }
  207         return _builder.fold_store(ptr, value, flags, AliasingGroup(0), 0);
  208       } else if (llvm::GetElementPtrInst* gep = llvm::dyn_cast<llvm::GetElementPtrInst>(inst)) {
  209         Value* ptr = lower_operand(gep->getPointerOperand());
  210         assert(ptr->type() == Type::Ptr);

@can-lehmann
can-lehmann merged commit b3fd2d3 into main Nov 18, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0