Skip to content

Commit

Permalink
hl: Refactor to remove code duplication.
Browse files Browse the repository at this point in the history
  • Loading branch information
frabert committed Jul 25, 2023
1 parent 16bafbd commit b8509f3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 75 deletions.
23 changes: 13 additions & 10 deletions include/vast/Dialect/HighLevel/HighLevelOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunctionType>:$function_type,
DefaultValuedAttr<GlobalLinkageKind, "GlobalLinkageKind::ExternalLinkage">:$linkage,
OptionalAttr<StrAttr>:$sym_visibility,
DefaultValuedAttr<GlobalLinkageKind, "GlobalLinkageKind::ExternalLinkage">:$linkage),
add_args,
(ins OptionalAttr<StrAttr>:$sym_visibility,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$res_attrs) >
OptionalAttr<DictArrayAttr>:$res_attrs)) >
{
let description = [{
Inspired by `cir::FuncOp` and `mlir::func::FuncOp`:
Expand All @@ -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<mlir::NamedAttribute>", "{}" >:$attrs,
CArg< "GlobalLinkageKind", "GlobalLinkageKind::ExternalLinkage" >:$linkage),
add_builders,
(ins CArg< "llvm::ArrayRef<mlir::NamedAttribute>", "{}" >:$attrs,
CArg< "llvm::ArrayRef<mlir::DictionaryAttr>", "{}" >:$arg_attrs,
CArg< "llvm::ArrayRef<mlir::DictionaryAttr>", "{}" >:$res_attrs,
CArg< "BuilderCallback", "std::nullopt" >:$body), [{
CArg< "BuilderCallback", "std::nullopt" >:$body)), !strconcat([{
InsertionGuard guard($_builder);
build_region($_builder, $_state, body);

Expand All @@ -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())
Expand All @@ -98,7 +101,7 @@ class FuncLikeOp< string mnemonic >
$_builder, $_state, arg_attrs, res_attrs,
getArgAttrsAttrName($_state.name), getResAttrsAttrName($_state.name)
);
}] >
}]) >
];

let extraClassDeclaration = [{
Expand Down
93 changes: 28 additions & 65 deletions include/vast/Dialect/HighLevel/HighLevelOpsCxx.td
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunctionType>:$function_type,
DefaultValuedAttr<GlobalLinkageKind, "GlobalLinkageKind::ExternalLinkage">:$linkage,
UnitAttr:$is_virtual,
UnitAttr:$is_const,
UnitAttr:$is_volatile,
RefQualifier:$ref,
OptionalAttr<StrAttr>:$sym_visibility,
OptionalAttr<DictArrayAttr>:$arg_attrs,
OptionalAttr<DictArrayAttr>:$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<mlir::NamedAttribute>", "{}" >:$attrs,
CArg< "llvm::ArrayRef<mlir::DictionaryAttr>", "{}" >:$arg_attrs,
CArg< "llvm::ArrayRef<mlir::DictionaryAttr>", "{}" >:$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)
}];
Expand Down

0 comments on commit b8509f3

Please sign in to comment.