Skip to content

[CIR] Refactor TryCallOp creation #704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,45 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
return createCallOp(loc, callee, mlir::cir::VoidType(), operands,
extraFnAttr);
}

mlir::cir::TryCallOp
createTryCallOp(mlir::Location loc, mlir::Value exception,
mlir::SymbolRefAttr callee = mlir::SymbolRefAttr(),
mlir::Type returnType = mlir::cir::VoidType(),
mlir::ValueRange operands = mlir::ValueRange(),
mlir::cir::ExtraFuncAttributesAttr extraFnAttr = {}) {
mlir::cir::TryCallOp tryCallOp = create<mlir::cir::TryCallOp>(
loc, callee, exception, returnType, operands);
if (extraFnAttr) {
tryCallOp->setAttr("extra_attrs", extraFnAttr);
} else {
mlir::NamedAttrList empty;
tryCallOp->setAttr("extra_attrs",
mlir::cir::ExtraFuncAttributesAttr::get(
getContext(), empty.getDictionary(getContext())));
}
return tryCallOp;
}

mlir::cir::TryCallOp
createTryCallOp(mlir::Location loc, mlir::cir::FuncOp callee,
mlir::Value exception, mlir::ValueRange operands,
mlir::cir::ExtraFuncAttributesAttr extraFnAttr = {}) {
return createTryCallOp(loc, exception, mlir::SymbolRefAttr::get(callee),
callee.getFunctionType().getReturnType(), operands,
extraFnAttr);
}

mlir::cir::TryCallOp createIndirectTryCallOp(mlir::Location loc,
mlir::Value ind_target,
mlir::Value exception,
mlir::cir::FuncType fn_type,
mlir::ValueRange operands) {
llvm::SmallVector<mlir::Value, 4> resOperands({ind_target});
resOperands.append(operands.begin(), operands.end());
return createTryCallOp(loc, exception, mlir::SymbolRefAttr(),
fn_type.getReturnType(), resOperands);
}
};

} // namespace cir
Expand Down
14 changes: 4 additions & 10 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -2881,14 +2881,6 @@ def TryCallOp : CIR_CallOp<"try_call"> {
let results = (outs Variadic<CIR_AnyType>);

let builders = [
OpBuilder<(ins "FuncOp":$callee, "mlir::Value":$exception,
CArg<"ValueRange", "{}">:$operands), [{
$_state.addOperands(ValueRange{exception});
$_state.addOperands(operands);
$_state.addAttribute("callee", SymbolRefAttr::get(callee));
if (!callee.getFunctionType().isVoid())
$_state.addTypes(callee.getFunctionType().getReturnType());
}]>,
OpBuilder<(ins "Value":$ind_target, "mlir::Value":$exception,
"FuncType":$fn_type,
CArg<"ValueRange", "{}">:$operands), [{
Expand All @@ -2903,8 +2895,10 @@ def TryCallOp : CIR_CallOp<"try_call"> {
[{
$_state.addOperands(ValueRange{exception});
$_state.addOperands(operands);
$_state.addAttribute("callee", callee);
$_state.addTypes(resType);
if (callee)
$_state.addAttribute("callee", callee);
if (resType && !isa<VoidType>(resType))
$_state.addTypes(resType);
}]>];

let hasVerifier = 1;
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/CIR/CodeGen/CIRGenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,11 @@ buildCallLikeOp(CIRGenFunction &CGF, mlir::Location callLoc,

mlir::cir::TryCallOp tryCallOp;
if (indirectFuncTy) {
tryCallOp = builder.create<mlir::cir::TryCallOp>(
tryCallOp = builder.createIndirectTryCallOp(
callLoc, addr, indirectFuncVal, indirectFuncTy, CIRCallArgs);
} else {
tryCallOp = builder.create<mlir::cir::TryCallOp>(callLoc, directFuncOp,
addr, CIRCallArgs);
tryCallOp =
builder.createTryCallOp(callLoc, directFuncOp, addr, CIRCallArgs);
}
tryCallOp->setAttr("extra_attrs", extraFnAttrs);
return tryCallOp;
Expand Down
Loading