From 23a033fd025b16397fb53cfb1570fdd158407ad1 Mon Sep 17 00:00:00 2001 From: Francesco Bertolaccini Date: Thu, 20 Jul 2023 12:44:11 +0200 Subject: [PATCH] hl: Refactor to remove code duplication. --- .../vast/Dialect/HighLevel/HighLevelOps.td | 23 +++-- .../vast/Dialect/HighLevel/HighLevelOpsCxx.td | 93 ++++++------------- 2 files changed, 41 insertions(+), 75 deletions(-) diff --git a/include/vast/Dialect/HighLevel/HighLevelOps.td b/include/vast/Dialect/HighLevel/HighLevelOps.td index 057441ffaf..8e321f251d 100644 --- a/include/vast/Dialect/HighLevel/HighLevelOps.td +++ b/include/vast/Dialect/HighLevel/HighLevelOps.td @@ -43,17 +43,18 @@ def ScopeOp : HighLevel_Op< "scope", [NoTerminator] > let assemblyFormat = [{ $body attr-dict }]; } -class FuncLikeOp< string mnemonic > +class FuncLikeOp< string mnemonic, dag add_args = (ins), dag add_builders = (ins), string add_init = "" > : HighLevel_Op< mnemonic, [ AutomaticAllocationScope, CallableOpInterface, FunctionOpInterface, IsolatedFromAbove, Symbol, NoTerminator ] > - , Arguments< (ins + , Arguments< !con((ins SymbolNameAttr:$sym_name, TypeAttrOf:$function_type, - DefaultValuedAttr:$linkage, - OptionalAttr:$sym_visibility, + DefaultValuedAttr:$linkage), + add_args, + (ins OptionalAttr:$sym_visibility, OptionalAttr:$arg_attrs, - OptionalAttr:$res_attrs) > + OptionalAttr:$res_attrs)) > { let description = [{ Inspired by `cir::FuncOp` and `mlir::func::FuncOp`: @@ -71,14 +72,15 @@ class FuncLikeOp< string mnemonic > let regions = (region AnyRegion:$body); let skipDefaultBuilders = 1; - let builders = [OpBuilder< (ins + let builders = [OpBuilder< !con((ins "llvm::StringRef":$name, "mlir::FunctionType":$type, - CArg< "GlobalLinkageKind", "GlobalLinkageKind::ExternalLinkage" >:$linkage, - CArg< "llvm::ArrayRef", "{}" >:$attrs, + CArg< "GlobalLinkageKind", "GlobalLinkageKind::ExternalLinkage" >:$linkage), + add_builders, + (ins CArg< "llvm::ArrayRef", "{}" >:$attrs, CArg< "llvm::ArrayRef", "{}" >:$arg_attrs, CArg< "llvm::ArrayRef", "{}" >:$res_attrs, - CArg< "BuilderCallback", "std::nullopt" >:$body), [{ + CArg< "BuilderCallback", "std::nullopt" >:$body)), !strconcat([{ InsertionGuard guard($_builder); build_region($_builder, $_state, body); @@ -89,6 +91,7 @@ class FuncLikeOp< string mnemonic > $_state.addAttribute( "linkage", GlobalLinkageKindAttr::get($_builder.getContext(), linkage) ); + }], add_init, [{ $_state.attributes.append(attrs.begin(), attrs.end()); if (arg_attrs.empty()) @@ -98,7 +101,7 @@ class FuncLikeOp< string mnemonic > $_builder, $_state, arg_attrs, res_attrs, getArgAttrsAttrName($_state.name), getResAttrsAttrName($_state.name) ); - }] > + }]) > ]; let extraClassDeclaration = [{ diff --git a/include/vast/Dialect/HighLevel/HighLevelOpsCxx.td b/include/vast/Dialect/HighLevel/HighLevelOpsCxx.td index 0681f2e1e1..6a18f70d60 100644 --- a/include/vast/Dialect/HighLevel/HighLevelOpsCxx.td +++ b/include/vast/Dialect/HighLevel/HighLevelOpsCxx.td @@ -102,74 +102,37 @@ def RefQualifier : I32EnumAttr< } def MethodOp - : FuncLikeOp< "method" > + : FuncLikeOp< "method", (ins + UnitAttr:$is_virtual, + UnitAttr:$is_const, + UnitAttr:$is_volatile, + RefQualifier:$ref), (ins + CArg< "bool", "false" >:$is_virtual, + CArg< "bool", "false" >:$is_const, + CArg< "bool", "false" >:$is_volatile, + CArg< "RefQualifier", "RefQualifier::ref_none" >:$ref), [{ + if (is_virtual) { + $_state.addAttribute( + "is_virtual", mlir::UnitAttr::get($_builder.getContext()) + ); + } + if (is_const) { + $_state.addAttribute( + "is_const", mlir::UnitAttr::get($_builder.getContext()) + ); + } + if (is_volatile) { + $_state.addAttribute( + "is_volatile", mlir::UnitAttr::get($_builder.getContext()) + ); + } + $_state.addAttribute( + "ref", RefQualifierAttr::get($_builder.getContext(), ref) + ); + }] > { let summary = "VAST high-level method definintion or declaration"; - let arguments = (ins - SymbolNameAttr:$sym_name, - TypeAttrOf:$function_type, - DefaultValuedAttr:$linkage, - UnitAttr:$is_virtual, - UnitAttr:$is_const, - UnitAttr:$is_volatile, - RefQualifier:$ref, - OptionalAttr:$sym_visibility, - OptionalAttr:$arg_attrs, - OptionalAttr:$res_attrs); - - let builders = [OpBuilder< (ins - "llvm::StringRef":$name, - "mlir::FunctionType":$type, - CArg< "GlobalLinkageKind", "GlobalLinkageKind::ExternalLinkage" >:$linkage, - CArg< "bool", "false" >:$is_virtual, - CArg< "bool", "false" >:$is_const, - CArg< "bool", "false" >:$is_volatile, - CArg< "RefQualifier", "RefQualifier::ref_none" >:$ref, - CArg< "llvm::ArrayRef", "{}" >:$attrs, - CArg< "llvm::ArrayRef", "{}" >:$arg_attrs, - CArg< "llvm::ArrayRef", "{}" >:$res_attrs, - CArg< "BuilderCallback", "std::nullopt" >:$body), [{ - InsertionGuard guard($_builder); - build_region($_builder, $_state, body); - - $_state.addAttribute( - mlir::SymbolTable::getSymbolAttrName(), $_builder.getStringAttr(name) - ); - $_state.addAttribute(getFunctionTypeAttrName($_state.name), mlir::TypeAttr::get(type)); - $_state.addAttribute( - "linkage", GlobalLinkageKindAttr::get($_builder.getContext(), linkage) - ); - if (is_virtual) { - $_state.addAttribute( - "is_virtual", mlir::UnitAttr::get($_builder.getContext()) - ); - } - if (is_const) { - $_state.addAttribute( - "is_const", mlir::UnitAttr::get($_builder.getContext()) - ); - } - if (is_volatile) { - $_state.addAttribute( - "is_volatile", mlir::UnitAttr::get($_builder.getContext()) - ); - } - $_state.addAttribute( - "ref", RefQualifierAttr::get($_builder.getContext(), ref) - ); - $_state.attributes.append(attrs.begin(), attrs.end()); - - if (arg_attrs.empty()) - return; - - mlir::function_interface_impl::addArgAndResultAttrs( - $_builder, $_state, arg_attrs, res_attrs, - getArgAttrsAttrName($_state.name), getResAttrsAttrName($_state.name) - ); - }] > - ]; - let assemblyFormat = [{ $linkage (`virtual` $is_virtual^)? $ref (`const` $is_const^)? (`volatile` $is_volatile^)? $sym_name custom< FunctionSignatureAndBody >($function_type, attr-dict, $body) }];