Skip to content

Commit 84daae6

Browse files
committed
WIP
1 parent 4ced0fb commit 84daae6

File tree

3 files changed

+53
-104
lines changed

3 files changed

+53
-104
lines changed

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

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2870,27 +2870,29 @@ def CIR_TryCallOp : CIR_CallOpBase<"try_call",[
28702870

28712871
```mlir
28722872
// Direct call
2873-
%result = cir.try_call @division(%a, %b) ^continue, ^landing_pad
2873+
%result = cir.try_call @division(%a, %b) ^normalDest, ^unwindDest
28742874
: (f32, f32) -> f32
28752875
```
28762876
}];
28772877

28782878
let arguments = !con((ins
2879-
Variadic<CIR_AnyType>:$contOperands,
2880-
Variadic<CIR_AnyType>:$landingPadOperands
2879+
Variadic<CIR_AnyType>:$normalDestOperands,
2880+
Variadic<CIR_AnyType>:$unwindDestOperands
28812881
), commonArgs);
28822882

28832883
let results = (outs Optional<CIR_AnyType>:$result);
2884-
let successors = (successor AnySuccessor:$cont, AnySuccessor:$landing_pad);
2884+
let successors = (successor AnySuccessor:$normalDest, AnySuccessor:$unwindDest);
28852885

28862886
let skipDefaultBuilders = 1;
2887+
let hasLLVMLowering = false;
28872888

28882889
let builders = [
28892890
OpBuilder<(ins "mlir::SymbolRefAttr":$callee, "mlir::Type":$resType,
2890-
"mlir::Block *":$cont, "mlir::Block *":$landing_pad,
2891+
"mlir::Block *":$normalDest,
2892+
"mlir::Block *":$unwindDest,
28912893
CArg<"mlir::ValueRange", "{}">:$operands,
2892-
CArg<"mlir::ValueRange", "{}">:$contOperands,
2893-
CArg<"mlir::ValueRange", "{}">:$landingPadOperands,
2894+
CArg<"mlir::ValueRange", "{}">:$normalDestOperands,
2895+
CArg<"mlir::ValueRange", "{}">:$unwindDestOperands,
28942896
CArg<"SideEffect", "SideEffect::All">:$sideEffect), [{
28952897
$_state.addOperands(operands);
28962898
if (callee)
@@ -2902,24 +2904,25 @@ def CIR_TryCallOp : CIR_CallOpBase<"try_call",[
29022904
SideEffectAttr::get($_builder.getContext(), sideEffect));
29032905

29042906
// Handle branches
2905-
$_state.addOperands(contOperands);
2906-
$_state.addOperands(landingPadOperands);
2907+
$_state.addOperands(normalDestOperands);
2908+
$_state.addOperands(unwindDestOperands);
29072909
// The TryCall ODS layout is: cont, landing_pad, operands.
29082910
llvm::copy(::llvm::ArrayRef<int32_t>({
2909-
static_cast<int32_t>(contOperands.size()),
2910-
static_cast<int32_t>(landingPadOperands.size()),
2911+
static_cast<int32_t>(normalDestOperands.size()),
2912+
static_cast<int32_t>(unwindDestOperands.size()),
29112913
static_cast<int32_t>(operands.size())
29122914
}),
29132915
odsState.getOrAddProperties<Properties>().operandSegmentSizes.begin());
2914-
$_state.addSuccessors(cont);
2915-
$_state.addSuccessors(landing_pad);
2916+
$_state.addSuccessors(normalDest);
2917+
$_state.addSuccessors(unwindDest);
29162918
}]>,
29172919
OpBuilder<(ins "mlir::Value":$ind_target,
29182920
"FuncType":$fn_type,
2919-
"mlir::Block *":$cont, "mlir::Block *":$landing_pad,
2921+
"mlir::Block *":$normalDest,
2922+
"mlir::Block *":$unwindDest,
29202923
CArg<"mlir::ValueRange", "{}">:$operands,
2921-
CArg<"mlir::ValueRange", "{}">:$contOperands,
2922-
CArg<"mlir::ValueRange", "{}">:$landingPadOperands,
2924+
CArg<"mlir::ValueRange", "{}">:$normalDestOperands,
2925+
CArg<"mlir::ValueRange", "{}">:$unwindDestOperands,
29232926
CArg<"SideEffect", "SideEffect::All">:$sideEffect), [{
29242927
::llvm::SmallVector<mlir::Value, 4> finalCallOperands({ind_target});
29252928
finalCallOperands.append(operands.begin(), operands.end());
@@ -2932,17 +2935,17 @@ def CIR_TryCallOp : CIR_CallOpBase<"try_call",[
29322935
SideEffectAttr::get($_builder.getContext(), sideEffect));
29332936

29342937
// Handle branches
2935-
$_state.addOperands(contOperands);
2936-
$_state.addOperands(landingPadOperands);
2938+
$_state.addOperands(normalDestOperands);
2939+
$_state.addOperands(unwindDestOperands);
29372940
// The TryCall ODS layout is: cont, landing_pad, operands.
29382941
llvm::copy(::llvm::ArrayRef<int32_t>({
2939-
static_cast<int32_t>(contOperands.size()),
2940-
static_cast<int32_t>(landingPadOperands.size()),
2942+
static_cast<int32_t>(normalDestOperands.size()),
2943+
static_cast<int32_t>(unwindDestOperands.size()),
29412944
static_cast<int32_t>(finalCallOperands.size())
29422945
}),
29432946
odsState.getOrAddProperties<Properties>().operandSegmentSizes.begin());
2944-
$_state.addSuccessors(cont);
2945-
$_state.addSuccessors(landing_pad);
2947+
$_state.addSuccessors(normalDest);
2948+
$_state.addSuccessors(unwindDest);
29462949
}]>
29472950
];
29482951
}

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 23 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -716,58 +716,21 @@ unsigned cir::CallOp::getNumArgOperands() {
716716
}
717717

718718
static mlir::ParseResult
719-
parseTryCallBranches(mlir::OpAsmParser &parser, mlir::OperationState &result,
720-
llvm::SmallVectorImpl<mlir::OpAsmParser::UnresolvedOperand>
721-
&continueOperands,
722-
llvm::SmallVectorImpl<mlir::OpAsmParser::UnresolvedOperand>
723-
&landingPadOperands,
724-
llvm::SmallVectorImpl<mlir::Type> &continueTypes,
725-
llvm::SmallVectorImpl<mlir::Type> &landingPadTypes,
726-
llvm::SMLoc &continueOperandsLoc,
727-
llvm::SMLoc &landingPadOperandsLoc) {
728-
mlir::Block *continueSuccessor = nullptr;
729-
mlir::Block *landingPadSuccessor = nullptr;
730-
731-
if (parser.parseSuccessor(continueSuccessor))
719+
parseTryCallDestinations(mlir::OpAsmParser &parser,
720+
mlir::OperationState &result) {
721+
mlir::Block *normalDestSuccessor;
722+
if (parser.parseSuccessor(normalDestSuccessor))
732723
return mlir::failure();
733724

734-
if (mlir::succeeded(parser.parseOptionalLParen())) {
735-
continueOperandsLoc = parser.getCurrentLocation();
736-
if (parser.parseOperandList(continueOperands))
737-
return mlir::failure();
738-
if (parser.parseColon())
739-
return mlir::failure();
740-
741-
if (parser.parseTypeList(continueTypes))
742-
return mlir::failure();
743-
if (parser.parseRParen())
744-
return mlir::failure();
745-
}
746-
747725
if (parser.parseComma())
748726
return mlir::failure();
749727

750-
if (parser.parseSuccessor(landingPadSuccessor))
751-
return mlir::failure();
752-
753-
if (mlir::succeeded(parser.parseOptionalLParen())) {
754-
landingPadOperandsLoc = parser.getCurrentLocation();
755-
if (parser.parseOperandList(landingPadOperands))
756-
return mlir::failure();
757-
if (parser.parseColon())
758-
return mlir::failure();
759-
760-
if (parser.parseTypeList(landingPadTypes))
761-
return mlir::failure();
762-
if (parser.parseRParen())
763-
return mlir::failure();
764-
}
765-
766-
if (parser.parseOptionalAttrDict(result.attributes))
728+
mlir::Block *unwindDestSuccessor;
729+
if (parser.parseSuccessor(unwindDestSuccessor))
767730
return mlir::failure();
768731

769-
result.addSuccessors(continueSuccessor);
770-
result.addSuccessors(landingPadSuccessor);
732+
result.addSuccessors(normalDestSuccessor);
733+
result.addSuccessors(unwindDestSuccessor);
771734
return mlir::success();
772735
}
773736

@@ -809,10 +772,7 @@ static mlir::ParseResult parseCallCommon(mlir::OpAsmParser &parser,
809772
return mlir::failure();
810773

811774
if (hasDestinationBlocks &&
812-
parseTryCallBranches(parser, result, continueOperands, landingPadOperands,
813-
continueTypes, landingPadTypes, continueOperandsLoc,
814-
landingPadOperandsLoc)
815-
.failed()) {
775+
parseTryCallDestinations(parser, result).failed()) {
816776
return ::mlir::failure();
817777
}
818778

@@ -874,8 +834,8 @@ static void printCallCommon(mlir::Operation *op,
874834
mlir::Value indirectCallee,
875835
mlir::OpAsmPrinter &printer, bool isNothrow,
876836
cir::SideEffect sideEffect,
877-
mlir::Block *cont = nullptr,
878-
mlir::Block *landingPad = nullptr) {
837+
mlir::Block *normalDest = nullptr,
838+
mlir::Block *unwindDest = nullptr) {
879839
printer << ' ';
880840

881841
auto callLikeOp = mlir::cast<cir::CIRCallOpInterface>(op);
@@ -892,28 +852,28 @@ static void printCallCommon(mlir::Operation *op,
892852

893853
printer << "(" << ops << ")";
894854

895-
if (cont) {
855+
if (normalDest) {
896856
assert(landingPad && "expected two successors");
897857
auto tryCall = dyn_cast<cir::TryCallOp>(op);
898858
assert(tryCall && "regular calls do not branch");
899-
printer << ' ' << tryCall.getCont();
900-
if (!tryCall.getContOperands().empty()) {
859+
printer << ' ' << tryCall.getNormalDest();
860+
if (!tryCall.getNormalDestOperands().empty()) {
901861
printer << "(";
902-
printer << tryCall.getContOperands();
862+
printer << tryCall.getNormalDestOperands();
903863
printer << ' ' << ":";
904864
printer << ' ';
905-
printer << tryCall.getContOperands().getTypes();
865+
printer << tryCall.getNormalDestOperands().getTypes();
906866
printer << ")";
907867
}
908868
printer << ",";
909869
printer << ' ';
910-
printer << tryCall.getLandingPad();
911-
if (!tryCall.getLandingPadOperands().empty()) {
870+
printer << tryCall.getUnwindDest();
871+
if (!tryCall.getUnwindDestOperands().empty()) {
912872
printer << "(";
913-
printer << tryCall.getLandingPadOperands();
873+
printer << tryCall.getUnwindDestOperands();
914874
printer << ' ' << ":";
915875
printer << ' ';
916-
printer << tryCall.getLandingPadOperands().getTypes();
876+
printer << tryCall.getUnwindDestOperands().getTypes();
917877
printer << ")";
918878
}
919879
}
@@ -1063,15 +1023,15 @@ void cir::TryCallOp::print(::mlir::OpAsmPrinter &p) {
10631023
mlir::Value indirectCallee = isIndirect() ? getIndirectCall() : nullptr;
10641024
cir::SideEffect sideEffect = getSideEffect();
10651025
printCallCommon(*this, getCalleeAttr(), indirectCallee, p, getNothrow(),
1066-
sideEffect, getCont(), getLandingPad());
1026+
sideEffect, getNormalDest(), getUnwindDest());
10671027
}
10681028

10691029
mlir::SuccessorOperands cir::TryCallOp::getSuccessorOperands(unsigned index) {
10701030
assert(index < getNumSuccessors() && "invalid successor index");
10711031
if (index == 0)
1072-
return SuccessorOperands(getContOperandsMutable());
1032+
return SuccessorOperands(getNormalDestOperandsMutable());
10731033
if (index == 1)
1074-
return SuccessorOperands(getLandingPadOperandsMutable());
1034+
return SuccessorOperands(getUnwindDestOperandsMutable());
10751035

10761036
// index == 2
10771037
return SuccessorOperands(getArgOperandsMutable());

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,9 +1478,7 @@ static mlir::LogicalResult
14781478
rewriteCallOrInvoke(mlir::Operation *op, mlir::ValueRange callOperands,
14791479
mlir::ConversionPatternRewriter &rewriter,
14801480
const mlir::TypeConverter *converter,
1481-
mlir::FlatSymbolRefAttr calleeAttr,
1482-
mlir::Block *continueBlock = nullptr,
1483-
mlir::Block *landingPadBlock = nullptr) {
1481+
mlir::FlatSymbolRefAttr calleeAttr) {
14841482
llvm::SmallVector<mlir::Type, 8> llvmResults;
14851483
mlir::ValueTypeRange<mlir::ResultRange> cirResults = op->getResultTypes();
14861484
auto call = cast<cir::CIRCallOpInterface>(op);
@@ -1509,7 +1507,7 @@ rewriteCallOrInvoke(mlir::Operation *op, mlir::ValueRange callOperands,
15091507
llvmFnTy = converter->convertType<mlir::LLVM::LLVMFunctionType>(
15101508
fn.getFunctionType());
15111509
assert(llvmFnTy && "Failed to convert function type");
1512-
} else if (auto alias = mlir::dyn_cast<mlir::LLVM::AliasOp>(callee)) {
1510+
} else if (auto alias = mlir::cast<mlir::LLVM::AliasOp>(callee)) {
15131511
// If the callee was an alias. In that case,
15141512
// we need to prepend the address of the alias to the operands. The
15151513
// way aliases work in the LLVM dialect is a little counter-intuitive.
@@ -1547,21 +1545,17 @@ rewriteCallOrInvoke(mlir::Operation *op, mlir::ValueRange callOperands,
15471545
converter->convertType(calleeFuncTy));
15481546
}
15491547

1548+
assert(!cir::MissingFeatures::opCallLandingPad());
1549+
assert(!cir::MissingFeatures::opCallContinueBlock());
15501550
assert(!cir::MissingFeatures::opCallCallConv());
15511551

1552-
if (landingPadBlock) {
1553-
rewriter.replaceOpWithNewOp<mlir::LLVM::InvokeOp>(
1554-
op, llvmFnTy, calleeAttr, callOperands, continueBlock,
1555-
mlir::ValueRange{}, landingPadBlock, mlir::ValueRange{});
1556-
return mlir::success();
1557-
}
1558-
15591552
auto newOp = rewriter.replaceOpWithNewOp<mlir::LLVM::CallOp>(
15601553
op, llvmFnTy, calleeAttr, callOperands);
15611554
if (memoryEffects)
15621555
newOp.setMemoryEffectsAttr(memoryEffects);
15631556
newOp.setNoUnwind(noUnwind);
15641557
newOp.setWillReturn(willReturn);
1558+
15651559
return mlir::success();
15661560
}
15671561

@@ -1572,14 +1566,6 @@ mlir::LogicalResult CIRToLLVMCallOpLowering::matchAndRewrite(
15721566
getTypeConverter(), op.getCalleeAttr());
15731567
}
15741568

1575-
mlir::LogicalResult CIRToLLVMTryCallOpLowering::matchAndRewrite(
1576-
cir::TryCallOp op, OpAdaptor adaptor,
1577-
mlir::ConversionPatternRewriter &rewriter) const {
1578-
return rewriteCallOrInvoke(op.getOperation(), adaptor.getOperands(), rewriter,
1579-
getTypeConverter(), op.getCalleeAttr(),
1580-
op.getCont(), op.getLandingPad());
1581-
}
1582-
15831569
mlir::LogicalResult CIRToLLVMReturnAddrOpLowering::matchAndRewrite(
15841570
cir::ReturnAddrOp op, OpAdaptor adaptor,
15851571
mlir::ConversionPatternRewriter &rewriter) const {

0 commit comments

Comments
 (0)