Skip to content
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
20 changes: 12 additions & 8 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ def CIR_ReturnOp : CIR_Op<"return", [
//===----------------------------------------------------------------------===//

def CIR_IfOp : CIR_Op<"if", [
DeclareOpInterfaceMethods<RegionBranchOpInterface>,
DeclareOpInterfaceMethods<RegionBranchOpInterface, ["getSuccessorInputs"]>,
RecursivelySpeculatable, AutomaticAllocationScope, NoRegionArguments
]> {
let summary = "the if-then-else operation";
Expand Down Expand Up @@ -1009,7 +1009,7 @@ def CIR_ResumeFlatOp : CIR_Op<"resume.flat", [
//===----------------------------------------------------------------------===//

def CIR_ScopeOp : CIR_Op<"scope", [
DeclareOpInterfaceMethods<RegionBranchOpInterface>,
DeclareOpInterfaceMethods<RegionBranchOpInterface, ["getSuccessorInputs"]>,
RecursivelySpeculatable, AutomaticAllocationScope, NoRegionArguments,
RecursiveMemoryEffects
]> {
Expand Down Expand Up @@ -1078,7 +1078,7 @@ def CIR_CaseOpKind : CIR_I32EnumAttr<"CaseOpKind", "case kind", [
]>;

def CIR_CaseOp : CIR_Op<"case", [
DeclareOpInterfaceMethods<RegionBranchOpInterface>,
DeclareOpInterfaceMethods<RegionBranchOpInterface, ["getSuccessorInputs"]>,
RecursivelySpeculatable, AutomaticAllocationScope
]> {
let summary = "Case operation";
Expand Down Expand Up @@ -1116,7 +1116,7 @@ def CIR_CaseOp : CIR_Op<"case", [

def CIR_SwitchOp : CIR_Op<"switch", [
SameVariadicOperandSize,
DeclareOpInterfaceMethods<RegionBranchOpInterface>,
DeclareOpInterfaceMethods<RegionBranchOpInterface, ["getSuccessorInputs"]>,
RecursivelySpeculatable, AutomaticAllocationScope, NoRegionArguments,
RecursiveMemoryEffects
]> {
Expand Down Expand Up @@ -1642,6 +1642,10 @@ class CIR_LoopOpBase<string mnemonic> : CIR_Op<mnemonic, [
llvm::SmallVectorImpl<mlir::RegionSuccessor> &regions) {
LoopOpInterface::getLoopOpSuccessorRegions(*this, point, regions);
}
ValueRange $cppClass::getSuccessorInputs(
mlir::RegionSuccessor successor) {
return LoopOpInterface::getLoopOpSuccessorInputs(*this, successor);
}
llvm::SmallVector<Region *> $cppClass::getLoopRegions() {
return {&getBody()};
}
Expand Down Expand Up @@ -2085,7 +2089,7 @@ def CIR_SelectOp : CIR_Op<"select", [
//===----------------------------------------------------------------------===//

def CIR_TernaryOp : CIR_Op<"ternary", [
DeclareOpInterfaceMethods<RegionBranchOpInterface>,
DeclareOpInterfaceMethods<RegionBranchOpInterface, ["getSuccessorInputs"]>,
RecursivelySpeculatable, AutomaticAllocationScope, NoRegionArguments
]> {
let summary = "The `cond ? a : b` C/C++ ternary operation";
Expand Down Expand Up @@ -2186,7 +2190,7 @@ def CIR_TLSModel : CIR_I32EnumAttr<"TLS_Model", "TLS model", [
]>;

def CIR_GlobalOp : CIR_Op<"global", [
DeclareOpInterfaceMethods<RegionBranchOpInterface>,
DeclareOpInterfaceMethods<RegionBranchOpInterface, ["getSuccessorInputs"]>,
DeclareOpInterfaceMethods<CIRGlobalValueInterface>,
NoRegionArguments
]> {
Expand Down Expand Up @@ -3304,7 +3308,7 @@ def CIR_AwaitKind : CIR_I32EnumAttr<"AwaitKind", "await kind", [
]>;

def CIR_AwaitOp : CIR_Op<"await",[
DeclareOpInterfaceMethods<RegionBranchOpInterface>,
DeclareOpInterfaceMethods<RegionBranchOpInterface, ["getSuccessorInputs"]>,
RecursivelySpeculatable, NoRegionArguments
]> {
let summary = "Wraps C++ co_await implicit logic";
Expand Down Expand Up @@ -5496,7 +5500,7 @@ def CIR_AllocExceptionOp : CIR_Op<"alloc.exception"> {
//===----------------------------------------------------------------------===//

def CIR_TryOp : CIR_Op<"try",[
DeclareOpInterfaceMethods<RegionBranchOpInterface>,
DeclareOpInterfaceMethods<RegionBranchOpInterface, ["getSuccessorInputs"]>,
RecursivelySpeculatable, AutomaticAllocationScope, NoRegionArguments
]> {
let summary = "C++ try block";
Expand Down
6 changes: 5 additions & 1 deletion clang/include/clang/CIR/Interfaces/CIRLoopOpInterface.td
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ include "mlir/Interfaces/ControlFlowInterfaces.td"
include "mlir/Interfaces/LoopLikeInterface.td"

def LoopOpInterface : OpInterface<"LoopOpInterface", [
DeclareOpInterfaceMethods<RegionBranchOpInterface>,
DeclareOpInterfaceMethods<RegionBranchOpInterface, ["getSuccessorInputs"]>,
DeclareOpInterfaceMethods<LoopLikeOpInterface>
]> {
let description = [{
Expand Down Expand Up @@ -88,6 +88,10 @@ def LoopOpInterface : OpInterface<"LoopOpInterface", [
static void getLoopOpSuccessorRegions(
::cir::LoopOpInterface op, ::mlir::RegionBranchPoint point,
::mlir::SmallVectorImpl<::mlir::RegionSuccessor> &regions);
/// Generic method to retrieve the successor inputs of a LoopOpInterface
/// operation.
static ::mlir::ValueRange getLoopOpSuccessorInputs(
::cir::LoopOpInterface op, ::mlir::RegionSuccessor successor);
}];

let verify = [{
Expand Down
64 changes: 52 additions & 12 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,14 @@ void cir::ConditionOp::getSuccessorRegions(

// Parent is a loop: condition may branch to the body or to the parent op.
if (auto loopOp = dyn_cast<LoopOpInterface>(getOperation()->getParentOp())) {
regions.emplace_back(&loopOp.getBody(), loopOp.getBody().getArguments());
regions.push_back(RegionSuccessor::parent(loopOp->getResults()));
regions.emplace_back(&loopOp.getBody());
regions.push_back(RegionSuccessor::parent());
}

// Parent is an await: condition may branch to resume or suspend regions.
auto await = cast<AwaitOp>(getOperation()->getParentOp());
regions.emplace_back(&await.getResume(), await.getResume().getArguments());
regions.emplace_back(&await.getSuspend(), await.getSuspend().getArguments());
regions.emplace_back(&await.getResume());
regions.emplace_back(&await.getSuspend());
}

MutableOperandRange
Expand Down Expand Up @@ -1168,7 +1168,7 @@ void cir::IfOp::getSuccessorRegions(mlir::RegionBranchPoint point,
SmallVectorImpl<RegionSuccessor> &regions) {
// The `then` and the `else` region branch back to the parent operation.
if (!point.isParent()) {
regions.push_back(RegionSuccessor::parent(getOperation()->getResults()));
regions.push_back(RegionSuccessor::parent());
return;
}

Expand All @@ -1186,6 +1186,10 @@ void cir::IfOp::getSuccessorRegions(mlir::RegionBranchPoint point,
return;
}

mlir::ValueRange cir::IfOp::getSuccessorInputs(RegionSuccessor successor) {
return successor.isParent() ? getOperation()->getResults() : ValueRange();
}

void cir::IfOp::build(OpBuilder &builder, OperationState &result, Value cond,
bool withElseRegion, BuilderCallbackRef thenBuilder,
BuilderCallbackRef elseBuilder) {
Expand Down Expand Up @@ -1218,14 +1222,18 @@ void cir::ScopeOp::getSuccessorRegions(
mlir::RegionBranchPoint point, SmallVectorImpl<RegionSuccessor> &regions) {
// The only region always branch back to the parent operation.
if (!point.isParent()) {
regions.push_back(RegionSuccessor::parent(getODSResults(0)));
regions.push_back(RegionSuccessor::parent());
return;
}

// If the condition isn't constant, both regions may be executed.
regions.push_back(RegionSuccessor(&getScopeRegion()));
}

mlir::ValueRange cir::ScopeOp::getSuccessorInputs(RegionSuccessor successor) {
return successor.isParent() ? getOperation()->getResults() : ValueRange();
}

void cir::ScopeOp::build(
OpBuilder &builder, OperationState &result,
function_ref<void(OpBuilder &, Type &, Location)> scopeBuilder) {
Expand Down Expand Up @@ -1383,12 +1391,16 @@ Block *cir::BrCondOp::getSuccessorForOperands(ArrayRef<Attribute> operands) {
void cir::CaseOp::getSuccessorRegions(
mlir::RegionBranchPoint point, SmallVectorImpl<RegionSuccessor> &regions) {
if (!point.isParent()) {
regions.push_back(RegionSuccessor::parent(getOperation()->getResults()));
regions.push_back(RegionSuccessor::parent());
return;
}
regions.push_back(RegionSuccessor(&getCaseRegion()));
}

mlir::ValueRange cir::CaseOp::getSuccessorInputs(RegionSuccessor successor) {
return successor.isParent() ? getOperation()->getResults() : ValueRange();
}

void cir::CaseOp::build(OpBuilder &builder, OperationState &result,
ArrayAttr value, CaseOpKind kind,
OpBuilder::InsertPoint &insertPoint) {
Expand All @@ -1409,13 +1421,17 @@ void cir::CaseOp::build(OpBuilder &builder, OperationState &result,
void cir::SwitchOp::getSuccessorRegions(
mlir::RegionBranchPoint point, SmallVectorImpl<RegionSuccessor> &region) {
if (!point.isParent()) {
region.push_back(RegionSuccessor::parent(getOperation()->getResults()));
region.push_back(RegionSuccessor::parent());
return;
}

region.push_back(RegionSuccessor(&getBody()));
}

mlir::ValueRange cir::SwitchOp::getSuccessorInputs(RegionSuccessor successor) {
return successor.isParent() ? getOperation()->getResults() : ValueRange();
}

void cir::SwitchOp::build(OpBuilder &builder, OperationState &result,
Value cond, BuilderOpStateCallbackRef switchBuilder) {
assert(switchBuilder && "the builder callback for regions must be present");
Expand Down Expand Up @@ -1623,7 +1639,7 @@ void cir::GlobalOp::getSuccessorRegions(
mlir::RegionBranchPoint point, SmallVectorImpl<RegionSuccessor> &regions) {
// The `ctor` and `dtor` regions always branch back to the parent operation.
if (!point.isParent()) {
regions.push_back(RegionSuccessor::parent(getOperation()->getResults()));
regions.push_back(RegionSuccessor::parent());
return;
}

Expand All @@ -1644,6 +1660,10 @@ void cir::GlobalOp::getSuccessorRegions(
regions.push_back(RegionSuccessor(dtorRegion));
}

mlir::ValueRange cir::GlobalOp::getSuccessorInputs(RegionSuccessor successor) {
return successor.isParent() ? getOperation()->getResults() : ValueRange();
}

static void printGlobalOpTypeAndInitialValue(OpAsmPrinter &p, cir::GlobalOp op,
TypeAttr type, Attribute initAttr,
mlir::Region &ctorRegion,
Expand Down Expand Up @@ -2308,7 +2328,7 @@ void cir::TernaryOp::getSuccessorRegions(
mlir::RegionBranchPoint point, SmallVectorImpl<RegionSuccessor> &regions) {
// The `true` and the `false` region branch back to the parent operation.
if (!point.isParent()) {
regions.push_back(RegionSuccessor::parent(this->getODSResults(0)));
regions.push_back(RegionSuccessor::parent());
return;
}

Expand All @@ -2318,6 +2338,10 @@ void cir::TernaryOp::getSuccessorRegions(
regions.push_back(RegionSuccessor(&getFalseRegion()));
}

mlir::ValueRange cir::TernaryOp::getSuccessorInputs(RegionSuccessor successor) {
return successor.isParent() ? getOperation()->getResults() : ValueRange();
}

void cir::TernaryOp::build(
OpBuilder &builder, OperationState &result, Value cond,
function_ref<void(OpBuilder &, Location)> trueBuilder,
Expand Down Expand Up @@ -2613,7 +2637,7 @@ void cir::AwaitOp::getSuccessorRegions(
// If any index all the underlying regions branch back to the parent
// operation.
if (!point.isParent()) {
regions.push_back(RegionSuccessor::parent(getOperation()->getResults()));
regions.push_back(RegionSuccessor::parent());
return;
}

Expand All @@ -2625,6 +2649,18 @@ void cir::AwaitOp::getSuccessorRegions(
regions.push_back(RegionSuccessor(&this->getResume()));
}

mlir::ValueRange cir::AwaitOp::getSuccessorInputs(RegionSuccessor successor) {
if (successor.isParent())
return getOperation()->getResults();
if (successor == &getReady())
return getReady().getArguments();
if (successor == &getSuspend())
return getSuspend().getArguments();
if (successor == &getResume())
return getResume().getArguments();
llvm_unreachable("invalid region successor");
}

LogicalResult cir::AwaitOp::verify() {
if (!isa<ConditionOp>(this->getReady().back().getTerminator()))
return emitOpError("ready region must end with cir.condition");
Expand Down Expand Up @@ -3527,7 +3563,7 @@ void cir::TryOp::getSuccessorRegions(
llvm::SmallVectorImpl<mlir::RegionSuccessor> &regions) {
// The `try` and the `catchers` region branch back to the parent operation.
if (!point.isParent()) {
regions.push_back(RegionSuccessor::parent(getOperation()->getResults()));
regions.push_back(RegionSuccessor::parent());
return;
}

Expand All @@ -3539,6 +3575,10 @@ void cir::TryOp::getSuccessorRegions(
regions.push_back(mlir::RegionSuccessor(&handlerRegion));
}

mlir::ValueRange cir::TryOp::getSuccessorInputs(RegionSuccessor successor) {
return successor.isParent() ? getOperation()->getResults() : ValueRange();
}

static void
printTryHandlerRegions(mlir::OpAsmPrinter &printer, cir::TryOp op,
mlir::MutableArrayRef<mlir::Region> handlerRegions,
Expand Down
28 changes: 23 additions & 5 deletions clang/lib/CIR/Interfaces/CIRLoopOpInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void LoopOpInterface::getLoopOpSuccessorRegions(

// Branching to first region: go to condition or body (do-while).
if (point.isParent()) {
regions.emplace_back(&op.getEntry(), op.getEntry().getArguments());
regions.emplace_back(&op.getEntry());
return;
}

Expand All @@ -30,8 +30,8 @@ void LoopOpInterface::getLoopOpSuccessorRegions(

// Branching from condition: go to body or exit.
if (&op.getCond() == parentRegion) {
regions.emplace_back(mlir::RegionSuccessor::parent(op->getResults()));
regions.emplace_back(&op.getBody(), op.getBody().getArguments());
regions.emplace_back(mlir::RegionSuccessor::parent());
regions.emplace_back(&op.getBody());
return;
}

Expand All @@ -40,19 +40,37 @@ void LoopOpInterface::getLoopOpSuccessorRegions(
// FIXME(cir): Should we consider break/continue statements here?
mlir::Region *afterBody =
(op.maybeGetStep() ? op.maybeGetStep() : &op.getCond());
regions.emplace_back(afterBody, afterBody->getArguments());
regions.emplace_back(afterBody);
return;
}

// Branching from step: go to condition.
if (op.maybeGetStep() == parentRegion) {
regions.emplace_back(&op.getCond(), op.getCond().getArguments());
regions.emplace_back(&op.getCond());
return;
}

llvm_unreachable("unexpected branch origin");
}

mlir::ValueRange
LoopOpInterface::getLoopOpSuccessorInputs(LoopOpInterface op,
mlir::RegionSuccessor successor) {
if (successor.isParent())
return op->getResults();
if (successor == &op.getEntry())
return op.getEntry().getArguments();
if (successor == &op.getBody())
return op.getBody().getArguments();
mlir::Region *afterBody =
(op.maybeGetStep() ? op.maybeGetStep() : &op.getCond());
if (successor == afterBody)
return afterBody->getArguments();
if (successor == &op.getCond())
return op.getCond().getArguments();
llvm_unreachable("invalid region successor");
}

/// Verify invariants of the LoopOpInterface.
llvm::LogicalResult detail::verifyLoopOpInterface(mlir::Operation *op) {
// FIXME: fix this so the conditionop isn't requiring MLIRCIR
Expand Down
3 changes: 2 additions & 1 deletion flang/include/flang/Optimizer/Dialect/FIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -2439,7 +2439,8 @@ def fir_IfOp
: region_Op<
"if", [DeclareOpInterfaceMethods<
RegionBranchOpInterface, ["getRegionInvocationBounds",
"getEntrySuccessorRegions"]>,
"getEntrySuccessorRegions",
"getSuccessorInputs"]>,
RecursiveMemoryEffects, NoRegionArguments,
WeightedRegionBranchOpInterface]> {
let summary = "if-then-else conditional operation";
Expand Down
15 changes: 10 additions & 5 deletions flang/lib/Optimizer/Dialect/FIROps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4699,7 +4699,7 @@ void fir::IfOp::getSuccessorRegions(
llvm::SmallVectorImpl<mlir::RegionSuccessor> &regions) {
// The `then` and the `else` region branch back to the parent operation.
if (!point.isParent()) {
regions.push_back(mlir::RegionSuccessor::parent(getResults()));
regions.push_back(mlir::RegionSuccessor::parent());
return;
}

Expand All @@ -4709,12 +4709,18 @@ void fir::IfOp::getSuccessorRegions(
// Don't consider the else region if it is empty.
mlir::Region *elseRegion = &this->getElseRegion();
if (elseRegion->empty())
regions.push_back(
mlir::RegionSuccessor::parent(getOperation()->getResults()));
regions.push_back(mlir::RegionSuccessor::parent());
else
regions.push_back(mlir::RegionSuccessor(elseRegion));
}

mlir::ValueRange
fir::IfOp::getSuccessorInputs(mlir::RegionSuccessor successor) {
if (successor.isParent())
return getOperation()->getResults();
return mlir::ValueRange();
}

void fir::IfOp::getEntrySuccessorRegions(
llvm::ArrayRef<mlir::Attribute> operands,
llvm::SmallVectorImpl<mlir::RegionSuccessor> &regions) {
Expand All @@ -4729,8 +4735,7 @@ void fir::IfOp::getEntrySuccessorRegions(
if (!getElseRegion().empty())
regions.emplace_back(&getElseRegion());
else
regions.push_back(
mlir::RegionSuccessor::parent(getOperation()->getResults()));
regions.push_back(mlir::RegionSuccessor::parent());
}
}

Expand Down
Loading