Skip to content

[mlir][NFC] Use hasOneBlock instead of llvm::hasSingleElement(region) #149809

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
Jul 24, 2025
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
2 changes: 1 addition & 1 deletion mlir/include/mlir/IR/OpDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ struct SingleBlock : public TraitBase<ConcreteType, SingleBlock> {
continue;

// Non-empty regions must contain a single basic block.
if (!llvm::hasSingleElement(region))
if (!region.hasOneBlock())
return op->emitOpError("expects region #")
<< i << " to have 0 or 1 blocks";

Expand Down
3 changes: 1 addition & 2 deletions mlir/include/mlir/Parser/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ inline OwningOpRef<ContainerOpT> constructContainerOpForParserIfNecessary(
OpBuilder builder(context);
ContainerOpT op = builder.create<ContainerOpT>(sourceFileLoc);
OwningOpRef<ContainerOpT> opRef(op);
assert(op->getNumRegions() == 1 &&
llvm::hasSingleElement(op->getRegion(0)) &&
assert(op->getNumRegions() == 1 && op->getRegion(0).hasOneBlock() &&
"expected generated operation to have a single region with a single "
"block");
Block *opBlock = &op->getRegion(0).front();
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Analysis/SliceAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static LogicalResult getBackwardSliceImpl(Operation *op,
// into us. For now, just bail.
if (parentOp && backwardSlice->count(parentOp) == 0) {
if (parentOp->getNumRegions() == 1 &&
llvm::hasSingleElement(parentOp->getRegion(0).getBlocks())) {
parentOp->getRegion(0).hasOneBlock()) {
return getBackwardSliceImpl(parentOp, visited, backwardSlice,
options);
}
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/Affine/IR/AffineOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ struct AffineInlinerInterface : public DialectInlinerInterface {

// Multi-block regions cannot be inlined into affine constructs, all of
// which require single-block regions.
if (!llvm::hasSingleElement(*src))
if (!src->hasOneBlock())
return false;

// Side-effecting operations that the affine dialect cannot understand
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/GPU/Transforms/EliminateBarriers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ getEffectsBefore(Operation *op,

// If there is a non-structured control flow, bail.
Region *region = op->getBlock()->getParent();
if (region && !llvm::hasSingleElement(region->getBlocks())) {
if (region && !region->hasOneBlock()) {
addAllValuelessEffects(effects);
return false;
}
Expand Down Expand Up @@ -250,7 +250,7 @@ getEffectsAfter(Operation *op,

// If there is a non-structured control flow, bail.
Region *region = op->getBlock()->getParent();
if (region && !llvm::hasSingleElement(region->getBlocks())) {
if (region && !region->hasOneBlock()) {
addAllValuelessEffects(effects);
return false;
}
Expand Down
3 changes: 1 addition & 2 deletions mlir/lib/Dialect/GPU/Transforms/MemoryPromotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ static void insertCopies(Region &region, Location loc, Value from, Value to) {
(void)toType;
assert(fromType.getShape() == toType.getShape());
assert(fromType.getRank() != 0);
assert(llvm::hasSingleElement(region) &&
"unstructured control flow not supported");
assert(region.hasOneBlock() && "unstructured control flow not supported");

auto b = ImplicitLocOpBuilder::atBlockBegin(loc, &region.front());
insertCopyLoops(b, from, to);
Expand Down
3 changes: 1 addition & 2 deletions mlir/lib/Dialect/Linalg/IR/LinalgInterfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1277,8 +1277,7 @@ LogicalResult mlir::linalg::detail::verifyStructuredOpInterface(Operation *op) {
return op->emitOpError("expected the shape-to-loops map to be non-null");

// Check the region has exactly one block.
if (linalgOp->getNumRegions() != 1 ||
!llvm::hasSingleElement(linalgOp->getRegion(0)))
if (linalgOp->getNumRegions() != 1 || !linalgOp->getRegion(0).hasOneBlock())
return op->emitOpError("expects to have 1 region with 1 block");

// Simplifying assumption: bbargs match 1-1 with shape operands elemental
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/Linalg/Utils/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static bool isTiled(AffineMap map, ArrayRef<OpFoldResult> tileSizes) {
std::optional<RegionMatcher::BinaryOpKind>
RegionMatcher::matchAsScalarBinaryOp(GenericOp op) {
auto &region = op.getRegion();
if (!llvm::hasSingleElement(region))
if (!region.hasOneBlock())
return std::nullopt;

Block &block = region.front();
Expand Down Expand Up @@ -204,7 +204,7 @@ bool allIndexingsAreProjectedPermutation(LinalgOp op) {
}

bool hasOnlyScalarElementwiseOp(Region &r) {
if (!llvm::hasSingleElement(r))
if (!r.hasOneBlock())
return false;
for (Operation &op : r.front()) {
if (!(isa<arith::ConstantOp, func::ConstantOp, tensor::ExtractOp,
Expand Down
2 changes: 1 addition & 1 deletion mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ struct RemoveConstantIfCondition : public OpRewritePattern<OpTy> {
/// using the operands of the block terminator to replace operation results.
static void replaceOpWithRegion(PatternRewriter &rewriter, Operation *op,
Region &region, ValueRange blockArgs = {}) {
assert(llvm::hasSingleElement(region) && "expected single-region block");
assert(region.hasOneBlock() && "expected single-block region");
Block *block = &region.front();
Operation *terminator = block->getTerminator();
ValueRange results = terminator->getOperands();
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/SCF/IR/SCF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ static TerminatorTy verifyAndGetTerminator(Operation *op, Region &region,
/// using the operands of the block terminator to replace operation results.
static void replaceOpWithRegion(PatternRewriter &rewriter, Operation *op,
Region &region, ValueRange blockArgs = {}) {
assert(llvm::hasSingleElement(region) && "expected single-region block");
assert(region.hasOneBlock() && "expected single-block region");
Block *block = &region.front();
Operation *terminator = block->getTerminator();
ValueRange results = terminator->getOperands();
Expand Down Expand Up @@ -184,7 +184,7 @@ struct SingleBlockExecuteInliner : public OpRewritePattern<ExecuteRegionOp> {

LogicalResult matchAndRewrite(ExecuteRegionOp op,
PatternRewriter &rewriter) const override {
if (!llvm::hasSingleElement(op.getRegion()))
if (!op.getRegion().hasOneBlock())
return failure();
replaceOpWithRegion(rewriter, op, op.getRegion());
return success();
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/SCF/TransformOps/SCFTransformOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ transform::LoopCoalesceOp::applyToOne(transform::TransformRewriter &rewriter,
/// using the operands of the block terminator to replace operation results.
static void replaceOpWithRegion(RewriterBase &rewriter, Operation *op,
Region &region) {
assert(llvm::hasSingleElement(region) && "expected single-region block");
assert(region.hasOneBlock() && "expected single-block region");
Block *block = &region.front();
Operation *terminator = block->getTerminator();
ValueRange results = terminator->getOperands();
Expand All @@ -434,7 +434,7 @@ DiagnosedSilenceableFailure transform::TakeAssumedBranchOp::applyToOne(
rewriter.setInsertionPoint(ifOp);
Region &region =
getTakeElseBranch() ? ifOp.getElseRegion() : ifOp.getThenRegion();
if (!llvm::hasSingleElement(region)) {
if (!region.hasOneBlock()) {
return emitDefiniteFailure()
<< "requires an scf.if op with a single-block "
<< ((getTakeElseBranch()) ? "`else`" : "`then`") << " region";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ static Value castBuffer(OpBuilder &b, Value buffer, Type type) {
static bool doesNotAliasExternalValue(Value value, Region *region,
ValueRange exceptions,
const OneShotAnalysisState &state) {
assert(llvm::hasSingleElement(region->getBlocks()) &&
"expected region with single block");
assert(region->hasOneBlock() && "expected region with single block");
bool result = true;
state.applyOnAliases(value, [&](Value alias) {
if (llvm::is_contained(exceptions, alias))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct AssumingOpInterface
size_t resultNum = std::distance(op->getOpResults().begin(),
llvm::find(op->getOpResults(), value));
// TODO: Support multiple blocks.
assert(llvm::hasSingleElement(assumingOp.getDoRegion().getBlocks()) &&
assert(assumingOp.getDoRegion().hasOneBlock() &&
"expected exactly 1 block");
auto yieldOp = dyn_cast<shape::AssumingYieldOp>(
assumingOp.getDoRegion().front().getTerminator());
Expand All @@ -49,8 +49,7 @@ struct AssumingOpInterface
const BufferizationOptions &options,
BufferizationState &state) const {
auto assumingOp = cast<shape::AssumingOp>(op);
assert(llvm::hasSingleElement(assumingOp.getDoRegion().getBlocks()) &&
"only 1 block supported");
assert(assumingOp.getDoRegion().hasOneBlock() && "only 1 block supported");
auto yieldOp = cast<shape::AssumingYieldOp>(
assumingOp.getDoRegion().front().getTerminator());

Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/IR/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ SymbolTable::SymbolTable(Operation *symbolTableOp)
"expected operation to have SymbolTable trait");
assert(symbolTableOp->getNumRegions() == 1 &&
"expected operation to have a single region");
assert(llvm::hasSingleElement(symbolTableOp->getRegion(0)) &&
assert(symbolTableOp->getRegion(0).hasOneBlock() &&
"expected operation to have a single block");

StringAttr symbolNameId = StringAttr::get(symbolTableOp->getContext(),
Expand Down Expand Up @@ -484,7 +484,7 @@ LogicalResult detail::verifySymbolTable(Operation *op) {
if (op->getNumRegions() != 1)
return op->emitOpError()
<< "Operations with a 'SymbolTable' must have exactly one region";
if (!llvm::hasSingleElement(op->getRegion(0)))
if (!op->getRegion(0).hasOneBlock())
return op->emitOpError()
<< "Operations with a 'SymbolTable' must have exactly one block";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ static LogicalResult inlineConvertOmpRegions(

// Special case for single-block regions that don't create additional blocks:
// insert operations without creating additional blocks.
if (llvm::hasSingleElement(region)) {
if (region.hasOneBlock()) {
llvm::Instruction *potentialTerminator =
builder.GetInsertBlock()->empty() ? nullptr
: &builder.GetInsertBlock()->back();
Expand Down
5 changes: 2 additions & 3 deletions mlir/lib/Transforms/CSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,8 @@ LogicalResult CSEDriver::simplifyOperation(ScopedMapTy &knownValues,

// Don't simplify operations with regions that have multiple blocks.
// TODO: We need additional tests to verify that we handle such IR correctly.
if (!llvm::all_of(op->getRegions(), [](Region &r) {
return r.getBlocks().empty() || llvm::hasSingleElement(r.getBlocks());
}))
if (!llvm::all_of(op->getRegions(),
[](Region &r) { return r.empty() || r.hasOneBlock(); }))
return failure();

// Some simple use case of operation with memory side-effect are dealt with
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Transforms/Utils/RegionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ static LogicalResult deleteDeadness(RewriterBase &rewriter,
for (Region &region : regions) {
if (region.empty())
continue;
bool hasSingleBlock = llvm::hasSingleElement(region);
bool hasSingleBlock = region.hasOneBlock();

// Delete every operation that is not live. Graph regions may have cycles
// in the use-def graph, so we must explicitly dropAllUses() from each
Expand Down Expand Up @@ -850,7 +850,7 @@ LogicalResult BlockMergeCluster::merge(RewriterBase &rewriter) {
/// failure otherwise.
static LogicalResult mergeIdenticalBlocks(RewriterBase &rewriter,
Region &region) {
if (region.empty() || llvm::hasSingleElement(region))
if (region.empty() || region.hasOneBlock())
return failure();

// Identify sets of blocks, other than the entry block, that branch to the
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/lib/Analysis/TestCFGLoopInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void TestCFGLoopInfo::runOnOperation() {
}
llvm::errs() << "\n";

if (llvm::hasSingleElement(region.getBlocks())) {
if (region.hasOneBlock()) {
llvm::errs() << "no loops\n";
return;
}
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/lib/Dialect/Test/TestPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2114,7 +2114,7 @@ struct TestMergeBlocksPatternDriver
/// Expect the op to have a single block after legalization.
target.addDynamicallyLegalOp<TestMergeBlocksOp>(
[&](TestMergeBlocksOp op) -> bool {
return llvm::hasSingleElement(op.getBody());
return op.getBody().hasOneBlock();
});

/// Only allow `test.br` within test.merge_blocks op.
Expand Down
Loading