From c3f6eeb6aae745841d47bf6e3eefa491ffad149d Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Tue, 28 May 2024 16:53:10 +0200 Subject: [PATCH 01/22] hl: Add Leaf and Cold attributes. --- include/vast/CodeGen/DefaultAttrVisitor.hpp | 2 ++ include/vast/Dialect/HighLevel/HighLevelAttributes.td | 2 ++ lib/vast/CodeGen/DefaultAttrVisitor.cpp | 8 ++++++++ 3 files changed, 12 insertions(+) diff --git a/include/vast/CodeGen/DefaultAttrVisitor.hpp b/include/vast/CodeGen/DefaultAttrVisitor.hpp index 911c244752..4b691a6b76 100644 --- a/include/vast/CodeGen/DefaultAttrVisitor.hpp +++ b/include/vast/CodeGen/DefaultAttrVisitor.hpp @@ -39,6 +39,8 @@ namespace vast::cg { mlir_attr VisitAsmLabelAttr(const clang::AsmLabelAttr *attr); mlir_attr VisitAllocAlignAttr(const clang::AllocAlignAttr *attr); mlir_attr VisitAllocSizeAttr(const clang::AllocSizeAttr *attr); + mlir_attr VisitLeafAttr(const clang::LeafAttr *attr); + mlir_attr VisitColdAttr(const clang::ColdAttr *attr); private: template< typename attr_t, typename... args_t > diff --git a/include/vast/Dialect/HighLevel/HighLevelAttributes.td b/include/vast/Dialect/HighLevel/HighLevelAttributes.td index 10129abbb8..68e90571a7 100644 --- a/include/vast/Dialect/HighLevel/HighLevelAttributes.td +++ b/include/vast/Dialect/HighLevel/HighLevelAttributes.td @@ -43,6 +43,8 @@ def WarnUnusedResAttr : HighLevel_Attr< "WarnUnusedResult", "warn_unused_result" def RestrictAttr : HighLevel_Attr< "Restrict", "restrict" >; def NoThrowAttr : HighLevel_Attr< "NoThrow", "nothrow" >; def NonNullAttr : HighLevel_Attr< "NonNull", "nonnull" >; +def LeafAttr : HighLevel_Attr< "Leaf", "leaf" >; +def ColdAttr : HighLevel_Attr< "Cold", "cold" >; def AsmLabelAttr : HighLevel_Attr< "AsmLabel", "asm" > { let parameters = (ins "::mlir::StringAttr":$label, "bool":$isLiteral); diff --git a/lib/vast/CodeGen/DefaultAttrVisitor.cpp b/lib/vast/CodeGen/DefaultAttrVisitor.cpp index 0656958277..7703495afe 100644 --- a/lib/vast/CodeGen/DefaultAttrVisitor.cpp +++ b/lib/vast/CodeGen/DefaultAttrVisitor.cpp @@ -78,4 +78,12 @@ namespace vast::cg auto num = attr->getNumElemsParam().isValid() ? attr->getNumElemsParam().getSourceIndex() : int(); return make< hl::AllocSizeAttr >(attr->getElemSizeParam().getSourceIndex(), num); } + + mlir_attr default_attr_visitor::VisitLeafAttr(const clang::LeafAttr *attr) { + return make< hl::LeafAttr >(); + } + + mlir_attr default_attr_visitor::VisitColdAttr(const clang::ColdAttr *attr) { + return make< hl::ColdAttr >(); + } } // namespace vast::hcg From 564df3e814d2854f7c27f799e105543a845eefa6 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Wed, 29 May 2024 10:23:27 +0200 Subject: [PATCH 02/22] hl: Add template class for attributes holding a number. --- .../Dialect/HighLevel/HighLevelAttributes.td | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/vast/Dialect/HighLevel/HighLevelAttributes.td b/include/vast/Dialect/HighLevel/HighLevelAttributes.td index 68e90571a7..403d68d812 100644 --- a/include/vast/Dialect/HighLevel/HighLevelAttributes.td +++ b/include/vast/Dialect/HighLevel/HighLevelAttributes.td @@ -30,6 +30,22 @@ class NameAttr let assemblyFormat = "`<` $name `>`"; } +class NumberAttr + : HighLevel_Attr< name, attr_mnemonic > +{ + let parameters = (ins "::mlir::IntegerAttr":$value); + let builders = [ + AttrBuilderWithInferredContext<( ins "::mlir::IntegerAttr":$value), [{ + return get(value.getContext(), value); + }]>, + AttrBuilder<(ins "::mlir::APSInt":$value), [{ + return get(mlir::IntegerAttr::get($_ctxt, value)); + }]>, + ]; + + let assemblyFormat = "`<` $value `>`"; +} + def AnnotationAttr : NameAttr< "Annotation", "annotation" >; def FormatAttr : NameAttr< "Format", "format" >; def SectionAttr : NameAttr< "Section", "section" >; From 179415dd52121db82e11afc634490d7d3ed371ba Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Wed, 29 May 2024 10:24:37 +0200 Subject: [PATCH 03/22] hl: Add hollow aligned attribute implementation. --- include/vast/CodeGen/DefaultAttrVisitor.hpp | 3 ++- include/vast/Dialect/HighLevel/HighLevelAttributes.td | 2 ++ lib/vast/CodeGen/DefaultAttrVisitor.cpp | 5 +++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/vast/CodeGen/DefaultAttrVisitor.hpp b/include/vast/CodeGen/DefaultAttrVisitor.hpp index 4b691a6b76..496990e791 100644 --- a/include/vast/CodeGen/DefaultAttrVisitor.hpp +++ b/include/vast/CodeGen/DefaultAttrVisitor.hpp @@ -25,6 +25,7 @@ namespace vast::cg { mlir_attr VisitSectionAttr(const clang::SectionAttr *attr); mlir_attr VisitFormatAttr(const clang::FormatAttr *attr); mlir_attr VisitAnnotateAttr(const clang::AnnotateAttr *attr); + mlir_attr VisitAlignedAttr(const clang::AlignedAttr *attr); mlir_attr VisitAlwaysInlineAttr(const clang::AlwaysInlineAttr *attr); mlir_attr VisitLoaderUninitializedAttr(const clang::LoaderUninitializedAttr *attr); mlir_attr VisitNoInstrumentFunctionAttr(const clang::NoInstrumentFunctionAttr *attr); @@ -49,4 +50,4 @@ namespace vast::cg { } }; -} // namespace vast::cg \ No newline at end of file +} // namespace vast::cg diff --git a/include/vast/Dialect/HighLevel/HighLevelAttributes.td b/include/vast/Dialect/HighLevel/HighLevelAttributes.td index 403d68d812..95cb048ac6 100644 --- a/include/vast/Dialect/HighLevel/HighLevelAttributes.td +++ b/include/vast/Dialect/HighLevel/HighLevelAttributes.td @@ -49,6 +49,8 @@ class NumberAttr def AnnotationAttr : NameAttr< "Annotation", "annotation" >; def FormatAttr : NameAttr< "Format", "format" >; def SectionAttr : NameAttr< "Section", "section" >; +// TODO(#595): Make aligned attribute keep the alignment value/expr +def AlignedAttr : HighLevel_Attr< "Aligned", "aligned" >; def AlwaysInlineAttr : HighLevel_Attr< "AlwaysInline", "always_inline" >; def ConstAttr : HighLevel_Attr< "Const", "const" >; def LoaderUninitAttr : HighLevel_Attr< "LoaderUninitialized", "loader_uninitialized" >; diff --git a/lib/vast/CodeGen/DefaultAttrVisitor.cpp b/lib/vast/CodeGen/DefaultAttrVisitor.cpp index 7703495afe..e5f8be8209 100644 --- a/lib/vast/CodeGen/DefaultAttrVisitor.cpp +++ b/lib/vast/CodeGen/DefaultAttrVisitor.cpp @@ -22,6 +22,11 @@ namespace vast::cg return make< hl::AnnotationAttr >(attr->getAnnotation()); } + mlir_attr default_attr_visitor::VisitAlignedAttr(const clang::AlignedAttr *attr) { + // TODO(#595): Keep the alignment in the attribute + return make< hl::AlignedAttr >(); + } + mlir_attr default_attr_visitor::VisitAlwaysInlineAttr(const clang::AlwaysInlineAttr *) { return make< hl::AlwaysInlineAttr >(); } From 1935cc57a1aaed01d96871f08656f25ba536c54c Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Wed, 29 May 2024 11:10:15 +0200 Subject: [PATCH 04/22] cg: Add visitor for implicit value init expr. --- include/vast/CodeGen/DefaultStmtVisitor.hpp | 1 + lib/vast/CodeGen/DefaultStmtVisitor.cpp | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/include/vast/CodeGen/DefaultStmtVisitor.hpp b/include/vast/CodeGen/DefaultStmtVisitor.hpp index a3eb6bfdd9..f29dfefa53 100644 --- a/include/vast/CodeGen/DefaultStmtVisitor.hpp +++ b/include/vast/CodeGen/DefaultStmtVisitor.hpp @@ -261,6 +261,7 @@ namespace vast::cg { operation VisitImaginaryLiteral(const clang::ImaginaryLiteral *lit); operation VisitInitListExpr(const clang::InitListExpr *expr); + operation VisitImplicitValueInitExpr(const clang::ImplicitValueInitExpr *expr); }; template< typename Op > diff --git a/lib/vast/CodeGen/DefaultStmtVisitor.cpp b/lib/vast/CodeGen/DefaultStmtVisitor.cpp index 728cc8af38..6edd67bc11 100644 --- a/lib/vast/CodeGen/DefaultStmtVisitor.cpp +++ b/lib/vast/CodeGen/DefaultStmtVisitor.cpp @@ -1009,4 +1009,12 @@ namespace vast::cg .freeze(); } + operation default_stmt_visitor::VisitImplicitValueInitExpr(const clang::ImplicitValueInitExpr *expr) { + return bld.compose< hl::InitListExpr >() + .bind(self.location(expr)) + .bind(self.visit(expr->getType())) + .bind(visit_values_range(expr->children())) + .freeze(); + } + } // namespace vast::cg From a6e0f757c85052b5bd27e9a02554501f4a18b806 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Wed, 29 May 2024 11:31:58 +0200 Subject: [PATCH 05/22] hl: Add Deprecated attribute. --- .../Dialect/HighLevel/HighLevelAttributes.td | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/include/vast/Dialect/HighLevel/HighLevelAttributes.td b/include/vast/Dialect/HighLevel/HighLevelAttributes.td index 95cb048ac6..496ea2ea0f 100644 --- a/include/vast/Dialect/HighLevel/HighLevelAttributes.td +++ b/include/vast/Dialect/HighLevel/HighLevelAttributes.td @@ -97,16 +97,28 @@ def BuiltinAttr : HighLevel_Attr< "Builtin", "builtin" > { let assemblyFormat = "`<` $ID `>`"; } -def AllocAlignAttr : HighLevel_Attr< "AllocAlign", "alloc_align" >{ +def AllocAlignAttr : HighLevel_Attr< "AllocAlign", "alloc_align" > { let parameters = (ins "int":$alignment); let assemblyFormat = "`<` $alignment `>`"; } -def AllocSizeAttr : HighLevel_Attr< "AllocSize", "alloc_size" >{ +def AllocSizeAttr : HighLevel_Attr< "AllocSize", "alloc_size" > { let parameters = (ins "int":$size_arg_pos, OptionalParameter< "int" >:$num_arg_pos); let assemblyFormat = "`<` `size_pos` `:` $size_arg_pos (`,` `num_pos` `:` $num_arg_pos^)? `>`"; } +def DeprecatedAttr : HighLevel_Attr< "Deprecated", "deprecated" > { + let parameters = (ins "::mlir::StringAttr":$message, + "::mlir::StringAttr":$fixit); + let assemblyFormat = "`<` `msg` `:` $message `,` `fix` `:` $fixit `>`"; + + let builders = [ + AttrBuilder<(ins "::mlir::StringRef":$msg, "::mlir::StringRef":$fix), [{ + return get($_ctxt, mlir::StringAttr::get($_ctxt, msg), mlir::StringAttr::get($_ctxt, fix)); + }]>, + ]; +} + #endif // VAST_DIALECT_HIGHLEVEL_IR_HIGHLEVELATTRIBUTES From 2895363652f615c03f58aff99bd8866133d8fe9a Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Wed, 29 May 2024 11:45:10 +0200 Subject: [PATCH 06/22] cg: Add Deprecated attribute visitor. --- include/vast/CodeGen/DefaultAttrVisitor.hpp | 1 + lib/vast/CodeGen/DefaultAttrVisitor.cpp | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/vast/CodeGen/DefaultAttrVisitor.hpp b/include/vast/CodeGen/DefaultAttrVisitor.hpp index 496990e791..65da227607 100644 --- a/include/vast/CodeGen/DefaultAttrVisitor.hpp +++ b/include/vast/CodeGen/DefaultAttrVisitor.hpp @@ -42,6 +42,7 @@ namespace vast::cg { mlir_attr VisitAllocSizeAttr(const clang::AllocSizeAttr *attr); mlir_attr VisitLeafAttr(const clang::LeafAttr *attr); mlir_attr VisitColdAttr(const clang::ColdAttr *attr); + mlir_attr VisitDeprecatedAttr(const clang::DeprecatedAttr *attr); private: template< typename attr_t, typename... args_t > diff --git a/lib/vast/CodeGen/DefaultAttrVisitor.cpp b/lib/vast/CodeGen/DefaultAttrVisitor.cpp index e5f8be8209..bee5519f71 100644 --- a/lib/vast/CodeGen/DefaultAttrVisitor.cpp +++ b/lib/vast/CodeGen/DefaultAttrVisitor.cpp @@ -91,4 +91,8 @@ namespace vast::cg mlir_attr default_attr_visitor::VisitColdAttr(const clang::ColdAttr *attr) { return make< hl::ColdAttr >(); } + + mlir_attr default_attr_visitor::VisitDeprecatedAttr(const clang::DeprecatedAttr *attr) { + return make< hl::DeprecatedAttr >(attr->getMessage(), attr->getReplacement()); + } } // namespace vast::hcg From 1851e88d25e673215397c8552903d601a1639c14 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Wed, 29 May 2024 13:25:21 +0200 Subject: [PATCH 07/22] test: Add test for deprecated attribute. --- test/vast/Dialect/HighLevel/attr-e.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 test/vast/Dialect/HighLevel/attr-e.c diff --git a/test/vast/Dialect/HighLevel/attr-e.c b/test/vast/Dialect/HighLevel/attr-e.c new file mode 100644 index 0000000000..bfe41a0d38 --- /dev/null +++ b/test/vast/Dialect/HighLevel/attr-e.c @@ -0,0 +1,12 @@ +// RUN: %vast-front -vast-emit-mlir=hl -o - %s | %file-check %s +// RUN: %vast-front -vast-emit-mlir=hl -o - %s > %t && %vast-opt %t | diff -B %t - + +// CHECK: hl.func @fun external () -> !hl.void attributes {deprecated = #hl.deprecated} { +__attribute__((deprecated("msg","fix"))) +void fun (void) {}; +// CHECK: hl.func @fun1 external () -> !hl.void attributes {deprecated = #hl.deprecated} { +__attribute__((deprecated("msg"))) +void fun1 (void) {}; +// CHECK: hl.func @fun2 external () -> !hl.void attributes {deprecated = #hl.deprecated} { +__attribute__((deprecated())) +void fun2 (void) {}; From 2c0b245dcb6d0e7a0f41b4ec18dd71e6f5a695be Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Wed, 29 May 2024 15:58:59 +0200 Subject: [PATCH 08/22] hl: Add transparent union attribute. --- include/vast/Dialect/HighLevel/HighLevelAttributes.td | 1 + 1 file changed, 1 insertion(+) diff --git a/include/vast/Dialect/HighLevel/HighLevelAttributes.td b/include/vast/Dialect/HighLevel/HighLevelAttributes.td index 496ea2ea0f..f995d18c33 100644 --- a/include/vast/Dialect/HighLevel/HighLevelAttributes.td +++ b/include/vast/Dialect/HighLevel/HighLevelAttributes.td @@ -63,6 +63,7 @@ def NoThrowAttr : HighLevel_Attr< "NoThrow", "nothrow" >; def NonNullAttr : HighLevel_Attr< "NonNull", "nonnull" >; def LeafAttr : HighLevel_Attr< "Leaf", "leaf" >; def ColdAttr : HighLevel_Attr< "Cold", "cold" >; +def TransparentUnionAttr : HighLevel_Attr< "TransparentUnion", "transparent_union" >; def AsmLabelAttr : HighLevel_Attr< "AsmLabel", "asm" > { let parameters = (ins "::mlir::StringAttr":$label, "bool":$isLiteral); From 76937a383e0faf9cef4271642f79afbeab565c76 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Wed, 29 May 2024 16:03:22 +0200 Subject: [PATCH 09/22] cg: Add transparent union attribute visitor. --- include/vast/CodeGen/DefaultAttrVisitor.hpp | 1 + lib/vast/CodeGen/DefaultAttrVisitor.cpp | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/vast/CodeGen/DefaultAttrVisitor.hpp b/include/vast/CodeGen/DefaultAttrVisitor.hpp index 65da227607..e7e316aced 100644 --- a/include/vast/CodeGen/DefaultAttrVisitor.hpp +++ b/include/vast/CodeGen/DefaultAttrVisitor.hpp @@ -43,6 +43,7 @@ namespace vast::cg { mlir_attr VisitLeafAttr(const clang::LeafAttr *attr); mlir_attr VisitColdAttr(const clang::ColdAttr *attr); mlir_attr VisitDeprecatedAttr(const clang::DeprecatedAttr *attr); + mlir_attr VisitTransparentUnionAttr(const clang::TransparentUnionAttr *attr); private: template< typename attr_t, typename... args_t > diff --git a/lib/vast/CodeGen/DefaultAttrVisitor.cpp b/lib/vast/CodeGen/DefaultAttrVisitor.cpp index bee5519f71..f437f5de70 100644 --- a/lib/vast/CodeGen/DefaultAttrVisitor.cpp +++ b/lib/vast/CodeGen/DefaultAttrVisitor.cpp @@ -95,4 +95,8 @@ namespace vast::cg mlir_attr default_attr_visitor::VisitDeprecatedAttr(const clang::DeprecatedAttr *attr) { return make< hl::DeprecatedAttr >(attr->getMessage(), attr->getReplacement()); } + + mlir_attr default_attr_visitor::VisitTransparentUnionAttr(const clang::TransparentUnionAttr *attr) { + return make< hl::TransparentUnionAttr >(); + } } // namespace vast::hcg From 605c446ee5346f745460efc77cf47150947a342b Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Wed, 29 May 2024 17:43:32 +0200 Subject: [PATCH 10/22] hl: Add compound literal op. --- include/vast/Dialect/HighLevel/HighLevelOps.td | 16 ++++++++++++++++ lib/vast/Dialect/HighLevel/HighLevelOps.cpp | 10 ++++++++++ 2 files changed, 26 insertions(+) diff --git a/include/vast/Dialect/HighLevel/HighLevelOps.td b/include/vast/Dialect/HighLevel/HighLevelOps.td index fa7db3437f..4653f3d59b 100644 --- a/include/vast/Dialect/HighLevel/HighLevelOps.td +++ b/include/vast/Dialect/HighLevel/HighLevelOps.td @@ -487,6 +487,22 @@ def UnreachableOp : HighLevel_Op<"unreachable", [Terminator]> { let assemblyFormat = "attr-dict"; } +def CompoundLiteralOp : HighLevel_Op< "compound_literal" > + , Results< (outs AnyType:$result) > +{ + let summary = "TODO"; + let description = [{ TODO }]; + let regions = (region ValueRegion:$init); + let skipDefaultBuilders = 1; + let builders = [ + OpBuilder<(ins + "Type":$type, + "builder_callback":$initbuilder + )> + ]; + let assemblyFormat = [{ attr-dict `:` type($result) `=` $init }]; +} + class CastKindAttr< string name, int val > : I64EnumAttrCase< name, val > {} class CastKindList< string name, string summary, list< CastKindAttr > cases > diff --git a/lib/vast/Dialect/HighLevel/HighLevelOps.cpp b/lib/vast/Dialect/HighLevel/HighLevelOps.cpp index f54c20aa6e..51981b6515 100644 --- a/lib/vast/Dialect/HighLevel/HighLevelOps.cpp +++ b/lib/vast/Dialect/HighLevel/HighLevelOps.cpp @@ -667,6 +667,16 @@ namespace vast::hl st.addTypes(type); } + void CompoundLiteralOp::build(Builder &bld, State &st, Type type, builder_callback init) + { + VAST_ASSERT(init && "the builder callback for 'init' region must be present"); + + InsertionGuard guard(bld); + + build_region(bld, st, builder_callback_ref(init)); + st.addTypes(type); + } + GRAPH_REGION_OP(FuncOp); GRAPH_REGION_OP(StmtExprOp); From b92744ae3474f75d4c31ff9ae7226e8a47194c04 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Wed, 29 May 2024 17:43:50 +0200 Subject: [PATCH 11/22] cg: Add compound literal expression visitor. --- lib/vast/CodeGen/DefaultStmtVisitor.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/vast/CodeGen/DefaultStmtVisitor.cpp b/lib/vast/CodeGen/DefaultStmtVisitor.cpp index 6edd67bc11..7f0cdc3f12 100644 --- a/lib/vast/CodeGen/DefaultStmtVisitor.cpp +++ b/lib/vast/CodeGen/DefaultStmtVisitor.cpp @@ -590,8 +590,12 @@ namespace vast::cg return {}; } - operation default_stmt_visitor::VisitCompoundLiteralExpr(const clang::CompoundLiteralExpr */* lit */) { - return {}; + operation default_stmt_visitor::VisitCompoundLiteralExpr(const clang::CompoundLiteralExpr *lit) { + return bld.compose< hl::CompoundLiteralOp >() + .bind(self.location(lit)) + .bind(self.visit(lit->getType())) + .bind(mk_value_builder(lit->getInitializer())) + .freeze(); } operation default_stmt_visitor::VisitFixedPointLiteral(const clang::FixedPointLiteral */* lit */) { From 646ccb460bd804b3d58e7942314798fdacaf50c4 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Wed, 29 May 2024 17:48:21 +0200 Subject: [PATCH 12/22] test: Add test for transparent_union. --- test/vast/Dialect/HighLevel/attr-f.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 test/vast/Dialect/HighLevel/attr-f.c diff --git a/test/vast/Dialect/HighLevel/attr-f.c b/test/vast/Dialect/HighLevel/attr-f.c new file mode 100644 index 0000000000..1531008969 --- /dev/null +++ b/test/vast/Dialect/HighLevel/attr-f.c @@ -0,0 +1,25 @@ +// RUN: %vast-front -vast-emit-mlir=hl -o - %s | %file-check %s +// RUN: %vast-front -vast-emit-mlir=hl -o - %s > %t && %vast-opt %t | diff -B %t - + +// CHECK: hl.union "u" {transparent_union = #hl.transparent_union} +union u { + int *a; + char *b; +} __attribute__ ((transparent_union)); + +void fun(union u x) { + return; +} + +int main() { + int x; +// CHECK: hl.call @fun({{.*}}) : (!hl.elaborated>) -> !hl.void + fun(&x); + char y; +// CHECK: hl.compound_literal : !hl.elaborated> +// CHECK: hl.call @fun({{.*}}) : (!hl.elaborated>) -> !hl.void + fun(&y); + union u z; +// CHECK: hl.call @fun({{.*}}) : (!hl.elaborated>) -> !hl.void + fun(z); +} From 803bfde898474aa7ae2babd0acc9a94a0d20346f Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Thu, 30 May 2024 11:13:17 +0200 Subject: [PATCH 13/22] hl: Add returns twice attribute. --- include/vast/Dialect/HighLevel/HighLevelAttributes.td | 1 + 1 file changed, 1 insertion(+) diff --git a/include/vast/Dialect/HighLevel/HighLevelAttributes.td b/include/vast/Dialect/HighLevel/HighLevelAttributes.td index f995d18c33..66589fbc09 100644 --- a/include/vast/Dialect/HighLevel/HighLevelAttributes.td +++ b/include/vast/Dialect/HighLevel/HighLevelAttributes.td @@ -64,6 +64,7 @@ def NonNullAttr : HighLevel_Attr< "NonNull", "nonnull" >; def LeafAttr : HighLevel_Attr< "Leaf", "leaf" >; def ColdAttr : HighLevel_Attr< "Cold", "cold" >; def TransparentUnionAttr : HighLevel_Attr< "TransparentUnion", "transparent_union" >; +def ReturnsTwiceAttr : HighLevel_Attr< "ReturnsTwice", "returns_twice" >; def AsmLabelAttr : HighLevel_Attr< "AsmLabel", "asm" > { let parameters = (ins "::mlir::StringAttr":$label, "bool":$isLiteral); From d265efa65d181e324c80a00c7040f23f52669830 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Thu, 30 May 2024 11:19:29 +0200 Subject: [PATCH 14/22] cg: Add returns twice attribute visitor. --- include/vast/CodeGen/DefaultAttrVisitor.hpp | 1 + lib/vast/CodeGen/DefaultAttrVisitor.cpp | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/vast/CodeGen/DefaultAttrVisitor.hpp b/include/vast/CodeGen/DefaultAttrVisitor.hpp index e7e316aced..68d1481838 100644 --- a/include/vast/CodeGen/DefaultAttrVisitor.hpp +++ b/include/vast/CodeGen/DefaultAttrVisitor.hpp @@ -44,6 +44,7 @@ namespace vast::cg { mlir_attr VisitColdAttr(const clang::ColdAttr *attr); mlir_attr VisitDeprecatedAttr(const clang::DeprecatedAttr *attr); mlir_attr VisitTransparentUnionAttr(const clang::TransparentUnionAttr *attr); + mlir_attr VisitReturnsTwiceAttr(const clang::ReturnsTwiceAttr *attr); private: template< typename attr_t, typename... args_t > diff --git a/lib/vast/CodeGen/DefaultAttrVisitor.cpp b/lib/vast/CodeGen/DefaultAttrVisitor.cpp index f437f5de70..ed0d22d215 100644 --- a/lib/vast/CodeGen/DefaultAttrVisitor.cpp +++ b/lib/vast/CodeGen/DefaultAttrVisitor.cpp @@ -99,4 +99,8 @@ namespace vast::cg mlir_attr default_attr_visitor::VisitTransparentUnionAttr(const clang::TransparentUnionAttr *attr) { return make< hl::TransparentUnionAttr >(); } + + mlir_attr default_attr_visitor::VisitReturnsTwiceAttr(const clang::ReturnsTwiceAttr *attr) { + return make< hl::ReturnsTwiceAttr >(); + } } // namespace vast::hcg From fca82e784c8cb523d4af4cb88319ec7b3eb484a4 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Thu, 30 May 2024 15:53:37 +0200 Subject: [PATCH 15/22] cg: Add helper for visiting type of literal expression with possibly lvalue type. --- include/vast/CodeGen/DefaultStmtVisitor.hpp | 4 ++++ lib/vast/CodeGen/DefaultStmtVisitor.cpp | 18 +++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/include/vast/CodeGen/DefaultStmtVisitor.hpp b/include/vast/CodeGen/DefaultStmtVisitor.hpp index f29dfefa53..ba5f93fe24 100644 --- a/include/vast/CodeGen/DefaultStmtVisitor.hpp +++ b/include/vast/CodeGen/DefaultStmtVisitor.hpp @@ -251,6 +251,10 @@ namespace vast::cg { // // Literals // + + // Helper for use with literal expressions that might return lvalue + mlir_type visit_maybe_lvalue_literal_type (const clang::Expr *expr); + operation VisitCharacterLiteral(const clang::CharacterLiteral *lit); operation VisitIntegerLiteral(const clang::IntegerLiteral *lit); operation VisitFloatingLiteral(const clang::FloatingLiteral *lit); diff --git a/lib/vast/CodeGen/DefaultStmtVisitor.cpp b/lib/vast/CodeGen/DefaultStmtVisitor.cpp index 7f0cdc3f12..7f0a14578a 100644 --- a/lib/vast/CodeGen/DefaultStmtVisitor.cpp +++ b/lib/vast/CodeGen/DefaultStmtVisitor.cpp @@ -562,6 +562,12 @@ namespace vast::cg // // Literals // + mlir_type default_stmt_visitor::visit_maybe_lvalue_literal_type (const clang::Expr *lit){ + if (lit->isLValue()) + return self.visit_as_lvalue_type(lit->getType()); + return self.visit(lit->getType()); + } + operation default_stmt_visitor::VisitCharacterLiteral(const clang::CharacterLiteral *lit) { return bld.constant(self.location(lit), self.visit(lit->getType()), apsint(lit->getValue())).getDefiningOp(); } @@ -575,15 +581,9 @@ namespace vast::cg } operation default_stmt_visitor::VisitStringLiteral(const clang::StringLiteral *lit) { - auto type = [&] { - if (lit->isLValue()) { - return self.visit_as_lvalue_type(lit->getType()); - } else { - return self.visit(lit->getType()); - } - }(); - - return bld.constant(self.location(lit), type, lit->getString()).getDefiningOp(); + return bld.constant(self.location(lit), + visit_maybe_lvalue_literal_type(lit), + lit->getString()).getDefiningOp(); } operation default_stmt_visitor::VisitUserDefinedLiteral(const clang::UserDefinedLiteral */* lit */) { From e8b4dfd785603f3387b3d82cec92d1b508fb7645 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Thu, 30 May 2024 15:54:17 +0200 Subject: [PATCH 16/22] cg: Fix CompoundLiteral not having lvalue in some cases. --- lib/vast/CodeGen/DefaultStmtVisitor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vast/CodeGen/DefaultStmtVisitor.cpp b/lib/vast/CodeGen/DefaultStmtVisitor.cpp index 7f0a14578a..ba47c3a4ac 100644 --- a/lib/vast/CodeGen/DefaultStmtVisitor.cpp +++ b/lib/vast/CodeGen/DefaultStmtVisitor.cpp @@ -593,7 +593,7 @@ namespace vast::cg operation default_stmt_visitor::VisitCompoundLiteralExpr(const clang::CompoundLiteralExpr *lit) { return bld.compose< hl::CompoundLiteralOp >() .bind(self.location(lit)) - .bind(self.visit(lit->getType())) + .bind(visit_maybe_lvalue_literal_type(lit)) .bind(mk_value_builder(lit->getInitializer())) .freeze(); } From 42963049c2d1622d5100fbab2057bc868f3226a2 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Thu, 30 May 2024 15:59:06 +0200 Subject: [PATCH 17/22] test: Add test for compound literal lvalue. --- test/vast/Dialect/HighLevel/literals-b.c | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 test/vast/Dialect/HighLevel/literals-b.c diff --git a/test/vast/Dialect/HighLevel/literals-b.c b/test/vast/Dialect/HighLevel/literals-b.c new file mode 100644 index 0000000000..f7636b37a0 --- /dev/null +++ b/test/vast/Dialect/HighLevel/literals-b.c @@ -0,0 +1,8 @@ +// // RUN: %vast-cc1 -vast-emit-mlir=hl %s -o - | %file-check %s +// // RUN: %vast-cc1 -vast-emit-mlir=hl %s -o %t && %vast-opt %t | diff -B %t - +struct S { + int arr[2]; +}; + +// CHECK: hl.compound_literal : !hl.lvalue +struct S *s = &(struct S){{[0] = 1, 1 + 1}}; From e87effff7bd1e073d6aa3fcd9dc2ce4189c3fa08 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Thu, 30 May 2024 16:17:01 +0200 Subject: [PATCH 18/22] cg: Imporve comment for for literal lvalue type helper. --- include/vast/CodeGen/DefaultStmtVisitor.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/include/vast/CodeGen/DefaultStmtVisitor.hpp b/include/vast/CodeGen/DefaultStmtVisitor.hpp index ba5f93fe24..6d9ede3b90 100644 --- a/include/vast/CodeGen/DefaultStmtVisitor.hpp +++ b/include/vast/CodeGen/DefaultStmtVisitor.hpp @@ -252,7 +252,10 @@ namespace vast::cg { // Literals // - // Helper for use with literal expressions that might return lvalue + // Helper for use with literal expressions that might return lvalue. + // It takes an expression, checks if it's lvalue expression + // and depending on the result calls either the lvalue type visitor or + // the standard type visitor. mlir_type visit_maybe_lvalue_literal_type (const clang::Expr *expr); operation VisitCharacterLiteral(const clang::CharacterLiteral *lit); From 8111889958da1e81891a2bbce76d7dd6685cc602 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Thu, 30 May 2024 16:17:31 +0200 Subject: [PATCH 19/22] hl: Remove unused number attribute helper. --- .../Dialect/HighLevel/HighLevelAttributes.td | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/include/vast/Dialect/HighLevel/HighLevelAttributes.td b/include/vast/Dialect/HighLevel/HighLevelAttributes.td index 66589fbc09..e67324a605 100644 --- a/include/vast/Dialect/HighLevel/HighLevelAttributes.td +++ b/include/vast/Dialect/HighLevel/HighLevelAttributes.td @@ -30,22 +30,6 @@ class NameAttr let assemblyFormat = "`<` $name `>`"; } -class NumberAttr - : HighLevel_Attr< name, attr_mnemonic > -{ - let parameters = (ins "::mlir::IntegerAttr":$value); - let builders = [ - AttrBuilderWithInferredContext<( ins "::mlir::IntegerAttr":$value), [{ - return get(value.getContext(), value); - }]>, - AttrBuilder<(ins "::mlir::APSInt":$value), [{ - return get(mlir::IntegerAttr::get($_ctxt, value)); - }]>, - ]; - - let assemblyFormat = "`<` $value `>`"; -} - def AnnotationAttr : NameAttr< "Annotation", "annotation" >; def FormatAttr : NameAttr< "Format", "format" >; def SectionAttr : NameAttr< "Section", "section" >; From 02c69f2333c3d3cb77d789f653e6a897a74c27ab Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Thu, 30 May 2024 16:18:26 +0200 Subject: [PATCH 20/22] hl: Cleanup DeprecatedAttr tablegen. --- include/vast/Dialect/HighLevel/HighLevelAttributes.td | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/include/vast/Dialect/HighLevel/HighLevelAttributes.td b/include/vast/Dialect/HighLevel/HighLevelAttributes.td index e67324a605..e71d2343c2 100644 --- a/include/vast/Dialect/HighLevel/HighLevelAttributes.td +++ b/include/vast/Dialect/HighLevel/HighLevelAttributes.td @@ -96,15 +96,17 @@ def AllocSizeAttr : HighLevel_Attr< "AllocSize", "alloc_size" > { } def DeprecatedAttr : HighLevel_Attr< "Deprecated", "deprecated" > { - let parameters = (ins "::mlir::StringAttr":$message, - "::mlir::StringAttr":$fixit); - let assemblyFormat = "`<` `msg` `:` $message `,` `fix` `:` $fixit `>`"; + let parameters = (ins + "::mlir::StringAttr":$message, + "::mlir::StringAttr":$fixit); let builders = [ AttrBuilder<(ins "::mlir::StringRef":$msg, "::mlir::StringRef":$fix), [{ return get($_ctxt, mlir::StringAttr::get($_ctxt, msg), mlir::StringAttr::get($_ctxt, fix)); }]>, ]; + + let assemblyFormat = "`<` `msg` `:` $message `,` `fix` `:` $fixit `>`"; } #endif // VAST_DIALECT_HIGHLEVEL_IR_HIGHLEVELATTRIBUTES From 831a7f28a99bfc0df11f4a6046e882ce646789d8 Mon Sep 17 00:00:00 2001 From: Robert Konicar Date: Thu, 30 May 2024 16:20:29 +0200 Subject: [PATCH 21/22] hl: Add description strings to CompoundLiteralOp. --- include/vast/Dialect/HighLevel/HighLevelOps.td | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/vast/Dialect/HighLevel/HighLevelOps.td b/include/vast/Dialect/HighLevel/HighLevelOps.td index 4653f3d59b..ab6d037ab9 100644 --- a/include/vast/Dialect/HighLevel/HighLevelOps.td +++ b/include/vast/Dialect/HighLevel/HighLevelOps.td @@ -490,8 +490,10 @@ def UnreachableOp : HighLevel_Op<"unreachable", [Terminator]> { def CompoundLiteralOp : HighLevel_Op< "compound_literal" > , Results< (outs AnyType:$result) > { - let summary = "TODO"; - let description = [{ TODO }]; + let summary = "VAST operation for building compound literals."; + let description = [{ Creates a compound literal that represents an unnamed object + and is initialzied by initializer-list.}]; + let regions = (region ValueRegion:$init); let skipDefaultBuilders = 1; let builders = [ From 5c15857dd21e5c5b35ae13008b8b515bce4bd367 Mon Sep 17 00:00:00 2001 From: xlauko Date: Fri, 31 May 2024 08:30:22 +0200 Subject: [PATCH 22/22] hl: Clean up tablegen code style. --- .../Dialect/HighLevel/HighLevelAttributes.td | 7 ++- .../vast/Dialect/HighLevel/HighLevelOps.td | 51 ++++++++++++------- 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/include/vast/Dialect/HighLevel/HighLevelAttributes.td b/include/vast/Dialect/HighLevel/HighLevelAttributes.td index e71d2343c2..e257eb5644 100644 --- a/include/vast/Dialect/HighLevel/HighLevelAttributes.td +++ b/include/vast/Dialect/HighLevel/HighLevelAttributes.td @@ -18,6 +18,7 @@ class NameAttr : HighLevel_Attr< name, attr_mnemonic > { let parameters = (ins "::mlir::StringAttr":$name); + let builders = [ AttrBuilderWithInferredContext<(ins "::mlir::StringAttr":$name), [{ return get(name.getContext(), name); @@ -52,6 +53,7 @@ def ReturnsTwiceAttr : HighLevel_Attr< "ReturnsTwice", "returns_twice" >; def AsmLabelAttr : HighLevel_Attr< "AsmLabel", "asm" > { let parameters = (ins "::mlir::StringAttr":$label, "bool":$isLiteral); + let builders = [ AttrBuilderWithInferredContext<(ins "::mlir::StringAttr":$label, "bool":$literal), [{ return get(label.getContext(), label, literal); @@ -66,6 +68,7 @@ def AsmLabelAttr : HighLevel_Attr< "AsmLabel", "asm" > { def ModeAttr : HighLevel_Attr< "Mode", "mode" > { let parameters = (ins "::mlir::StringAttr":$mode); + let builders = [ AttrBuilderWithInferredContext<(ins "::mlir::StringAttr":$mode), [{ return get(mode.getContext(), mode); @@ -80,6 +83,7 @@ def ModeAttr : HighLevel_Attr< "Mode", "mode" > { def BuiltinAttr : HighLevel_Attr< "Builtin", "builtin" > { let parameters = (ins "unsigned":$ID); + let assemblyFormat = "`<` $ID `>`"; } @@ -98,7 +102,8 @@ def AllocSizeAttr : HighLevel_Attr< "AllocSize", "alloc_size" > { def DeprecatedAttr : HighLevel_Attr< "Deprecated", "deprecated" > { let parameters = (ins "::mlir::StringAttr":$message, - "::mlir::StringAttr":$fixit); + "::mlir::StringAttr":$fixit + ); let builders = [ AttrBuilder<(ins "::mlir::StringRef":$msg, "::mlir::StringRef":$fix), [{ diff --git a/include/vast/Dialect/HighLevel/HighLevelOps.td b/include/vast/Dialect/HighLevel/HighLevelOps.td index ab6d037ab9..5e6c373809 100644 --- a/include/vast/Dialect/HighLevel/HighLevelOps.td +++ b/include/vast/Dialect/HighLevel/HighLevelOps.td @@ -468,9 +468,15 @@ def ConstantOp def InitializedConstantOp : HighLevel_Op< "const.init", [ConstantLike, Pure] > , Results< (outs AnyType:$result) > { - let summary = "VAST value constant"; - let description = [{ VAST value constant witch requires region initialization }]; + let summary = "VAST constant with non-trivial initialization"; + let description = [{ + InitializedConstantOp is used to represent a constant with non-trivial + initialization. The initialization is represented by a region that + represents initialization expression. + }]; + let regions = (region ValueRegion:$init); + let skipDefaultBuilders = 1; let builders = [ OpBuilder<(ins @@ -478,6 +484,7 @@ def InitializedConstantOp : HighLevel_Op< "const.init", [ConstantLike, Pure] > "builder_callback":$initbuilder )> ]; + let assemblyFormat = [{ attr-dict `:` type($result) `=` $init }]; } @@ -490,11 +497,16 @@ def UnreachableOp : HighLevel_Op<"unreachable", [Terminator]> { def CompoundLiteralOp : HighLevel_Op< "compound_literal" > , Results< (outs AnyType:$result) > { - let summary = "VAST operation for building compound literals."; - let description = [{ Creates a compound literal that represents an unnamed object - and is initialzied by initializer-list.}]; + let summary = "VAST compound literals operation"; + let description = [{ + Creates a compound literal that represents an unnamed object and is + initialzied by initializer-list. + + Is used to represetnt `clang::CompoundLiteralExpr`. + }]; let regions = (region ValueRegion:$init); + let skipDefaultBuilders = 1; let builders = [ OpBuilder<(ins @@ -502,6 +514,7 @@ def CompoundLiteralOp : HighLevel_Op< "compound_literal" > "builder_callback":$initbuilder )> ]; + let assemblyFormat = [{ attr-dict `:` type($result) `=` $init }]; } @@ -763,12 +776,12 @@ class StandardArithBinOp< string mnemonic, list< Trait > traits = [] > class FloatArithBinOp< string mnemonic, list< Trait > traits = [] > : ArithBinOp< mnemonic, traits > { - let hasVerifier = 1; - let extraClassDefinition = [{ + let hasVerifier = 1; + let extraClassDefinition = [{ logical_result $cppClass::verify() { - return verify_float_arith_op(this->getOperation()); + return verify_float_arith_op(this->getOperation()); } - }]; + }]; } def AddIOp : ArithBinOp< "add", [Commutative, IsAdditive< "lhs", "rhs", "result" >] >; @@ -1048,6 +1061,7 @@ def FCmpOp { let summary = "VAST flaoting point comparison operation"; let description = [{ VAST floating point comparison operation }]; + let hasVerifier = 1; let assemblyFormat = "$predicate $lhs `,` $rhs attr-dict `:` type(operands) `->` type($result)"; @@ -1288,8 +1302,8 @@ def IdentKind : IdentKindList< "IdentKind", "ident kind", [ def PredefinedExpr : HighLevel_Op< "predefined.expr" > - , Arguments<(ins AnyType:$value, IdentKind:$kind)> - , Results<(outs AnyType:$result)> + , Arguments<(ins AnyType:$value, IdentKind:$kind)> + , Results<(outs AnyType:$result)> { let summary = "VAT predefined expr ( such as __func__ )"; let description = [{ VAT predefined expr ( such as __func__ ) }]; @@ -1299,8 +1313,8 @@ def PredefinedExpr def ExtensionOp : HighLevel_Op< "gnu.extension" > - , Arguments<(ins AnyType:$value)> - , Results<(outs AnyType:$result)> + , Arguments<(ins AnyType:$value)> + , Results<(outs AnyType:$result)> { let summary = "VAST extension (__extension__) keyword"; let description = [{ VAST op corresponding to GNU __extension__ keyword. }]; @@ -1332,11 +1346,12 @@ def AsmOp )> { let summary = "VAST operation for inline assembly"; - let description = [{ VAST operation mirroring the GCCAsmStmt in clang AST. It prints - a name for every operand (either its id or user-supplied string).}]; + let description = [{ + VAST operation mirroring the GCCAsmStmt in clang AST. It prints a name for + every operand (either its id or user-supplied string). + }]; let skipDefaultBuilders = 1; - let builders = [ OpBuilder< (ins "mlir::StringAttr":$asm_template, @@ -1353,7 +1368,9 @@ def AsmOp ) > ]; - let assemblyFormat = [{attr-dict $asm_template `(`($output_names $asm_outputs^ `:` $output_constraints)? `)` `(` (`ins` `:`$input_names $asm_inputs^ `:` $input_constraints)? `)` `(`( $clobbers^)?`)` `(`( $labels^)?`)` `:` functional-type(operands, results)}]; + let assemblyFormat = [{ + attr-dict $asm_template `(`($output_names $asm_outputs^ `:` $output_constraints)? `)` `(` (`ins` `:`$input_names $asm_inputs^ `:` $input_constraints)? `)` `(`( $clobbers^)?`)` `(`( $labels^)?`)` `:` functional-type(operands, results) + }]; } def VAArgExpr