From 0af27be7c68f11a6223228e5415f6d230bcc96b1 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Wed, 11 Sep 2024 13:42:54 +0200 Subject: [PATCH 1/2] cg: Add ast context to attribute visitor. --- include/vast/CodeGen/DefaultAttrVisitor.hpp | 5 +++++ lib/vast/CodeGen/DefaultVisitor.cpp | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/vast/CodeGen/DefaultAttrVisitor.hpp b/include/vast/CodeGen/DefaultAttrVisitor.hpp index 77e0a13f3c..1827206abd 100644 --- a/include/vast/CodeGen/DefaultAttrVisitor.hpp +++ b/include/vast/CodeGen/DefaultAttrVisitor.hpp @@ -17,6 +17,9 @@ namespace vast::cg { using base = attr_visitor_base< default_attr_visitor >; using base::base; + default_attr_visitor(mcontext_t &mctx, acontext_t &actx, codegen_builder &bld, visitor_view self, scope_context &scope) + : base(mctx, bld, self, scope), actx(actx) {} + using attr_visitor_base< default_attr_visitor >::Visit; mlir_attr visit(const clang_attr *attr) { return Visit(attr); } @@ -68,6 +71,8 @@ namespace vast::cg { auto make(args_t &&...args) { return bld.getAttr< attr_t >(std::forward< args_t >(args)...); } + + acontext_t &actx; }; } // namespace vast::cg diff --git a/lib/vast/CodeGen/DefaultVisitor.cpp b/lib/vast/CodeGen/DefaultVisitor.cpp index b80c1a7fec..be647c4cf2 100644 --- a/lib/vast/CodeGen/DefaultVisitor.cpp +++ b/lib/vast/CodeGen/DefaultVisitor.cpp @@ -26,7 +26,7 @@ namespace vast::cg } std::optional< named_attr > default_visitor::visit(const clang_attr *attr, scope_context &scope) { - default_attr_visitor visitor(mctx, bld, self, scope); + default_attr_visitor visitor(mctx, actx, bld, self, scope); if (auto visited = visitor.visit(attr)) { auto name = visited.getAbstractAttribute().getName(); return std::make_optional< named_attr >( From fe251eb1f25bc7325015b9aa809c13a4b218585f Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Wed, 11 Sep 2024 13:43:26 +0200 Subject: [PATCH 2/2] cg: Do constant evaluation on assume aligned attribute parameters. --- lib/vast/CodeGen/DefaultAttrVisitor.cpp | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/vast/CodeGen/DefaultAttrVisitor.cpp b/lib/vast/CodeGen/DefaultAttrVisitor.cpp index 706a1ae42e..da5e16a2c8 100644 --- a/lib/vast/CodeGen/DefaultAttrVisitor.cpp +++ b/lib/vast/CodeGen/DefaultAttrVisitor.cpp @@ -158,23 +158,20 @@ namespace vast::cg } mlir_attr default_attr_visitor::VisitAssumeAlignedAttr(const clang::AssumeAlignedAttr *attr) { - llvm::APInt alignment, offset; - if (auto alignment_literal = mlir::dyn_cast< clang::IntegerLiteral >(attr->getAlignment())) { - alignment = alignment_literal->getValue(); - } else { - VAST_REPORT("assume_aligned attribute with non-trivial expression is not supported"); + auto alignment = attr->getAlignment()->getIntegerConstantExpr(actx); + if (!alignment) { + VAST_REPORT("could not evaluate assume aligned attribute alignment value."); return {}; } - auto offset_expr = attr->getOffset(); - if (offset_expr) { - if (auto offset_literal = mlir::dyn_cast< clang::IntegerLiteral >(offset_expr)) { - offset = offset_literal->getValue(); - } else { - VAST_REPORT("assume_aligned attribute with non-trivial expression is not supported"); - return {}; + if (auto offset_expr = attr->getOffset()) { + auto offset = offset_expr->getIntegerConstantExpr(actx); + if (!offset) { + VAST_REPORT("could not evaluate assume aligned attribute offest value."); + return {}; } + return make< hl::AssumeAlignedAttr >(alignment.value(), offset.value()); } - return make< hl::AssumeAlignedAttr >(alignment, offset); + return make< hl::AssumeAlignedAttr >(alignment.value(), llvm::APInt()); } mlir_attr default_attr_visitor::VisitCountedByAttr(const clang::CountedByAttr *attr) {