Skip to content

Commit 2c3b8d9

Browse files
roro47lanza
authored andcommitted
[CIR] Refactor TryCallOp creation (llvm#704)
1 parent 84dc9c3 commit 2c3b8d9

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

+39
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,45 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
504504
return createCallOp(loc, callee, mlir::cir::VoidType(), operands,
505505
extraFnAttr);
506506
}
507+
508+
mlir::cir::TryCallOp
509+
createTryCallOp(mlir::Location loc, mlir::Value exception,
510+
mlir::SymbolRefAttr callee = mlir::SymbolRefAttr(),
511+
mlir::Type returnType = mlir::cir::VoidType(),
512+
mlir::ValueRange operands = mlir::ValueRange(),
513+
mlir::cir::ExtraFuncAttributesAttr extraFnAttr = {}) {
514+
mlir::cir::TryCallOp tryCallOp = create<mlir::cir::TryCallOp>(
515+
loc, callee, exception, returnType, operands);
516+
if (extraFnAttr) {
517+
tryCallOp->setAttr("extra_attrs", extraFnAttr);
518+
} else {
519+
mlir::NamedAttrList empty;
520+
tryCallOp->setAttr("extra_attrs",
521+
mlir::cir::ExtraFuncAttributesAttr::get(
522+
getContext(), empty.getDictionary(getContext())));
523+
}
524+
return tryCallOp;
525+
}
526+
527+
mlir::cir::TryCallOp
528+
createTryCallOp(mlir::Location loc, mlir::cir::FuncOp callee,
529+
mlir::Value exception, mlir::ValueRange operands,
530+
mlir::cir::ExtraFuncAttributesAttr extraFnAttr = {}) {
531+
return createTryCallOp(loc, exception, mlir::SymbolRefAttr::get(callee),
532+
callee.getFunctionType().getReturnType(), operands,
533+
extraFnAttr);
534+
}
535+
536+
mlir::cir::TryCallOp createIndirectTryCallOp(mlir::Location loc,
537+
mlir::Value ind_target,
538+
mlir::Value exception,
539+
mlir::cir::FuncType fn_type,
540+
mlir::ValueRange operands) {
541+
llvm::SmallVector<mlir::Value, 4> resOperands({ind_target});
542+
resOperands.append(operands.begin(), operands.end());
543+
return createTryCallOp(loc, exception, mlir::SymbolRefAttr(),
544+
fn_type.getReturnType(), resOperands);
545+
}
507546
};
508547

509548
} // namespace cir

clang/include/clang/CIR/Dialect/IR/CIROps.td

+4-10
Original file line numberDiff line numberDiff line change
@@ -2881,14 +2881,6 @@ def TryCallOp : CIR_CallOp<"try_call"> {
28812881
let results = (outs Variadic<CIR_AnyType>);
28822882

28832883
let builders = [
2884-
OpBuilder<(ins "FuncOp":$callee, "mlir::Value":$exception,
2885-
CArg<"ValueRange", "{}">:$operands), [{
2886-
$_state.addOperands(ValueRange{exception});
2887-
$_state.addOperands(operands);
2888-
$_state.addAttribute("callee", SymbolRefAttr::get(callee));
2889-
if (!callee.getFunctionType().isVoid())
2890-
$_state.addTypes(callee.getFunctionType().getReturnType());
2891-
}]>,
28922884
OpBuilder<(ins "Value":$ind_target, "mlir::Value":$exception,
28932885
"FuncType":$fn_type,
28942886
CArg<"ValueRange", "{}">:$operands), [{
@@ -2903,8 +2895,10 @@ def TryCallOp : CIR_CallOp<"try_call"> {
29032895
[{
29042896
$_state.addOperands(ValueRange{exception});
29052897
$_state.addOperands(operands);
2906-
$_state.addAttribute("callee", callee);
2907-
$_state.addTypes(resType);
2898+
if (callee)
2899+
$_state.addAttribute("callee", callee);
2900+
if (resType && !isa<VoidType>(resType))
2901+
$_state.addTypes(resType);
29082902
}]>];
29092903

29102904
let hasVerifier = 1;

clang/lib/CIR/CodeGen/CIRGenCall.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,11 @@ buildCallLikeOp(CIRGenFunction &CGF, mlir::Location callLoc,
456456

457457
mlir::cir::TryCallOp tryCallOp;
458458
if (indirectFuncTy) {
459-
tryCallOp = builder.create<mlir::cir::TryCallOp>(
459+
tryCallOp = builder.createIndirectTryCallOp(
460460
callLoc, addr, indirectFuncVal, indirectFuncTy, CIRCallArgs);
461461
} else {
462-
tryCallOp = builder.create<mlir::cir::TryCallOp>(callLoc, directFuncOp,
463-
addr, CIRCallArgs);
462+
tryCallOp =
463+
builder.createTryCallOp(callLoc, directFuncOp, addr, CIRCallArgs);
464464
}
465465
tryCallOp->setAttr("extra_attrs", extraFnAttrs);
466466
return tryCallOp;

0 commit comments

Comments
 (0)