-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CIR][Interfaces] Implement LoopOpInterface
Adds an interface to generically handle lowering and analysis of loop operations in CIR. It can also perform verification of invariants common to all loop operations. ghstack-source-id: 0e413b14ea063a2b0d75aeaca0af88e547c15277 Pull Request resolved: #405
- Loading branch information
1 parent
5186df1
commit e9fdf3a
Showing
17 changed files
with
237 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===- CIRLoopOpInterface.h - Interface for CIR loop-like ops --*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===---------------------------------------------------------------------===// | ||
// | ||
// Defines the interface to generically handle CIR loop operations. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef CLANG_INTERFACES_CIR_CIRLOOPOPINTERFACE_H_ | ||
#define CLANG_INTERFACES_CIR_CIRLOOPOPINTERFACE_H_ | ||
|
||
#include "mlir/IR/BuiltinTypes.h" | ||
#include "mlir/IR/OpDefinition.h" | ||
#include "mlir/IR/Operation.h" | ||
#include "mlir/Interfaces/ControlFlowInterfaces.h" | ||
#include "mlir/Interfaces/LoopLikeInterface.h" | ||
|
||
namespace mlir { | ||
namespace cir { | ||
namespace detail { | ||
|
||
/// Verify invariants of the LoopOpInterface. | ||
::mlir::LogicalResult verifyLoopOpInterface(::mlir::Operation *op); | ||
|
||
} // namespace detail | ||
} // namespace cir | ||
} // namespace mlir | ||
|
||
/// Include the tablegen'd interface declarations. | ||
#include "clang/CIR/Interfaces/CIRLoopOpInterface.h.inc" | ||
|
||
#endif // CLANG_INTERFACES_CIR_CIRLOOPOPINTERFACE_H_ |
100 changes: 100 additions & 0 deletions
100
clang/include/clang/CIR/Interfaces/CIRLoopOpInterface.td
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//===- CIRLoopOpInterface.td - Interface for CIR loop-like ops -*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===---------------------------------------------------------------------===// | ||
|
||
#ifndef CLANG_CIR_INTERFACES_CIRLOOPOPINTERFACE | ||
#define CLANG_CIR_INTERFACES_CIRLOOPOPINTERFACE | ||
|
||
include "mlir/IR/OpBase.td" | ||
include "mlir/Interfaces/ControlFlowInterfaces.td" | ||
include "mlir/Interfaces/LoopLikeInterface.td" | ||
|
||
def LoopOpInterface : OpInterface<"LoopOpInterface", [ | ||
DeclareOpInterfaceMethods<RegionBranchOpInterface>, | ||
DeclareOpInterfaceMethods<LoopLikeOpInterface> | ||
]> { | ||
let description = [{ | ||
Contains helper functions to query properties and perform transformations | ||
on a loop. | ||
}]; | ||
let cppNamespace = "::mlir::cir"; | ||
|
||
let methods = [ | ||
InterfaceMethod<[{ | ||
Returns the loop's conditional region. | ||
}], | ||
/*retTy=*/"mlir::Region &", | ||
/*methodName=*/"getCond" | ||
>, | ||
InterfaceMethod<[{ | ||
Returns the loop's body region. | ||
}], | ||
/*retTy=*/"mlir::Region &", | ||
/*methodName=*/"getBody" | ||
>, | ||
InterfaceMethod<[{ | ||
Returns a pointer to the loop's step region or nullptr. | ||
}], | ||
/*retTy=*/"mlir::Region *", | ||
/*methodName=*/"maybeGetStep", | ||
/*args=*/(ins), | ||
/*methodBody=*/"", | ||
/*defaultImplementation=*/"return nullptr;" | ||
>, | ||
InterfaceMethod<[{ | ||
Returns the first region to be executed in the loop. | ||
}], | ||
/*retTy=*/"mlir::Region &", | ||
/*methodName=*/"getEntry", | ||
/*args=*/(ins), | ||
/*methodBody=*/"", | ||
/*defaultImplementation=*/"return $_op.getCond();" | ||
>, | ||
InterfaceMethod<[{ | ||
Returns a list of regions in order of execution. | ||
}], | ||
/*retTy=*/"llvm::SmallVector<mlir::Region *>", | ||
/*methodName=*/"getRegionsInExecutionOrder", | ||
/*args=*/(ins), | ||
/*methodBody=*/"", | ||
/*defaultImplementation=*/[{ | ||
return llvm::SmallVector<mlir::Region *, 2>{&$_op.getRegion(0), &$_op.getRegion(1)}; | ||
}] | ||
>, | ||
InterfaceMethod<[{ | ||
Recursively walks the body of the loop in pre-order while skipping | ||
nested loops and executing a callback on every other operation. | ||
}], | ||
/*retTy=*/"mlir::WalkResult", | ||
/*methodName=*/"walkBodySkippingNestedLoops", | ||
/*args=*/(ins "::llvm::function_ref<void (Operation *)>":$callback), | ||
/*methodBody=*/"", | ||
/*defaultImplementation=*/[{ | ||
return $_op.getBody().template walk<WalkOrder::PreOrder>([&](Operation *op) { | ||
if (isa<LoopOpInterface>(op)) | ||
return mlir::WalkResult::skip(); | ||
callback(op); | ||
return mlir::WalkResult::advance(); | ||
}); | ||
}] | ||
> | ||
]; | ||
let extraClassDeclaration = [{ | ||
/// Generic method to retrieve the successors of a LoopOpInterface operation. | ||
static void getLoopOpSuccessorRegions( | ||
::mlir::cir::LoopOpInterface op, ::mlir::RegionBranchPoint point, | ||
::mlir::SmallVectorImpl<::mlir::RegionSuccessor> ®ions); | ||
}]; | ||
let verify = [{ | ||
/// Verify invariants of the LoopOpInterface. | ||
return detail::verifyLoopOpInterface($_op); | ||
}]; | ||
} | ||
#endif // CLANG_CIR_INTERFACES_CIRLOOPOPINTERFACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//===- CIRLoopOpInterface.cpp - Interface for CIR loop-like ops *- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===---------------------------------------------------------------------===// | ||
|
||
#include "clang/CIR/Interfaces/CIRLoopOpInterface.h" | ||
|
||
#include "clang/CIR/Dialect/IR/CIRDialect.h" | ||
#include "clang/CIR/Interfaces/CIRLoopOpInterface.cpp.inc" | ||
#include "llvm/Support/ErrorHandling.h" | ||
|
||
namespace mlir { | ||
namespace cir { | ||
|
||
void LoopOpInterface::getLoopOpSuccessorRegions( | ||
LoopOpInterface op, RegionBranchPoint point, | ||
SmallVectorImpl<RegionSuccessor> ®ions) { | ||
assert(point.isParent() || point.getRegionOrNull()); | ||
|
||
// Branching to first region: go to condition or body (do-while). | ||
if (point.isParent()) { | ||
regions.emplace_back(&op.getEntry(), op.getEntry().getArguments()); | ||
} | ||
// Branching from condition: go to body or exit. | ||
else if (&op.getCond() == point.getRegionOrNull()) { | ||
regions.emplace_back(RegionSuccessor(op->getResults())); | ||
regions.emplace_back(&op.getBody(), op.getBody().getArguments()); | ||
} | ||
// Branching from body: go to step (for) or condition. | ||
else if (&op.getBody() == point.getRegionOrNull()) { | ||
// FIXME(cir): Should we consider break/continue statements here? | ||
auto *afterBody = (op.maybeGetStep() ? op.maybeGetStep() : &op.getCond()); | ||
regions.emplace_back(afterBody, afterBody->getArguments()); | ||
} | ||
// Branching from step: go to condition. | ||
else if (op.maybeGetStep() == point.getRegionOrNull()) { | ||
regions.emplace_back(&op.getCond(), op.getCond().getArguments()); | ||
} | ||
} | ||
|
||
/// Verify invariants of the LoopOpInterface. | ||
LogicalResult detail::verifyLoopOpInterface(Operation *op) { | ||
auto loopOp = cast<LoopOpInterface>(op); | ||
if (!isa<ConditionOp>(loopOp.getCond().back().getTerminator())) | ||
return op->emitOpError( | ||
"expected condition region to terminate with 'cir.condition'"); | ||
return success(); | ||
} | ||
|
||
} // namespace cir | ||
} // namespace mlir |
Oops, something went wrong.