Skip to content

Commit 61dd481

Browse files
[MLIR][LLVMDialect] SelectionOp conversion pattern
This patch introduces conversion pattern for `spv.selection` op. The conversion can only be applied to selection with all blocks being reachable. Moreover, selection with control attributes "Flatten" and "DontFlatten" is not supported. Since the `PatternRewriter` hook for block merging has not been implemented for `ConversionPatternRewriter`, merge and continue blocks are kept separately. Reviewed By: antiagainst, ftynse Differential Revision: https://reviews.llvm.org/D83860
1 parent 7b5bddf commit 61dd481

2 files changed

Lines changed: 169 additions & 0 deletions

File tree

‎mlir/lib/Conversion/SPIRVToLLVM/ConvertSPIRVToLLVM.cpp‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,84 @@ class ReturnValuePattern : public SPIRVToLLVMConversion<spirv::ReturnValueOp> {
613613
}
614614
};
615615

616+
class MergePattern : public SPIRVToLLVMConversion<spirv::MergeOp> {
617+
public:
618+
using SPIRVToLLVMConversion<spirv::MergeOp>::SPIRVToLLVMConversion;
619+
620+
LogicalResult
621+
matchAndRewrite(spirv::MergeOp op, ArrayRef<Value> operands,
622+
ConversionPatternRewriter &rewriter) const override {
623+
rewriter.eraseOp(op);
624+
return success();
625+
}
626+
};
627+
628+
/// Converts `spv.selection` with `spv.BranchConditional` in its header block.
629+
/// All blocks within selection should be reachable for conversion to succeed.
630+
class SelectionPattern : public SPIRVToLLVMConversion<spirv::SelectionOp> {
631+
public:
632+
using SPIRVToLLVMConversion<spirv::SelectionOp>::SPIRVToLLVMConversion;
633+
634+
LogicalResult
635+
matchAndRewrite(spirv::SelectionOp op, ArrayRef<Value> operands,
636+
ConversionPatternRewriter &rewriter) const override {
637+
// There is no support for `Flatten` or `DontFlatten` selection control at
638+
// the moment. This are just compiler hints and can be performed during the
639+
// optimization passes.
640+
if (op.selection_control() != spirv::SelectionControl::None)
641+
return failure();
642+
643+
// `spv.selection` should have at least two blocks: one selection header
644+
// block and one merge block. If no blocks are present, or control flow
645+
// branches straight to merge block (two blocks are present), the op is
646+
// redundant and it is erased.
647+
if (op.body().getBlocks().size() <= 2) {
648+
rewriter.eraseOp(op);
649+
return success();
650+
}
651+
652+
Location loc = op.getLoc();
653+
654+
// Split the current block after `spv.selection`. The remaing ops will be
655+
// used in `continueBlock`.
656+
auto *currentBlock = rewriter.getInsertionBlock();
657+
rewriter.setInsertionPointAfter(op);
658+
auto position = rewriter.getInsertionPoint();
659+
auto *continueBlock = rewriter.splitBlock(currentBlock, position);
660+
661+
// Extract conditional branch information from the header block. By SPIR-V
662+
// dialect spec, it should contain `spv.BranchConditional` or `spv.Switch`
663+
// op. Note that `spv.Switch op` is not supported at the moment in the
664+
// SPIR-V dialect. Remove this block when finished.
665+
auto *headerBlock = op.getHeaderBlock();
666+
assert(headerBlock->getOperations().size() == 1);
667+
auto condBrOp = dyn_cast<spirv::BranchConditionalOp>(
668+
headerBlock->getOperations().front());
669+
if (!condBrOp)
670+
return failure();
671+
rewriter.eraseBlock(headerBlock);
672+
673+
// Branch from merge block to continue block.
674+
auto *mergeBlock = op.getMergeBlock();
675+
Operation *terminator = mergeBlock->getTerminator();
676+
ValueRange terminatorOperands = terminator->getOperands();
677+
rewriter.setInsertionPointToEnd(mergeBlock);
678+
rewriter.create<LLVM::BrOp>(loc, terminatorOperands, continueBlock);
679+
680+
// Link current block to `true` and `false` blocks within the selection.
681+
Block *trueBlock = condBrOp.getTrueBlock();
682+
Block *falseBlock = condBrOp.getFalseBlock();
683+
rewriter.setInsertionPointToEnd(currentBlock);
684+
rewriter.create<LLVM::CondBrOp>(loc, condBrOp.condition(), trueBlock,
685+
condBrOp.trueTargetOperands(), falseBlock,
686+
condBrOp.falseTargetOperands());
687+
688+
rewriter.inlineRegionBefore(op.body(), continueBlock);
689+
rewriter.replaceOp(op, continueBlock->getArguments());
690+
return success();
691+
}
692+
};
693+
616694
/// Converts SPIR-V shift ops to LLVM shift ops. Since LLVM dialect
617695
/// puts a restriction on `Shift` and `Base` to have the same bit width,
618696
/// `Shift` is zero or sign extended to match this specification. Cases when
@@ -843,6 +921,7 @@ void mlir::populateSPIRVToLLVMConversionPatterns(
843921

844922
// Control Flow ops
845923
BranchConversionPattern, BranchConditionalConversionPattern,
924+
SelectionPattern, MergePattern,
846925

847926
// Function Call op
848927
FunctionCallPattern,

‎mlir/test/Conversion/SPIRVToLLVM/control-flow-ops-to-llvm.mlir‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,93 @@ spv.module Logical GLSL450 {
8080
spv.Return
8181
}
8282
}
83+
84+
// -----
85+
86+
//===----------------------------------------------------------------------===//
87+
// spv.selection
88+
//===----------------------------------------------------------------------===//
89+
90+
spv.module Logical GLSL450 {
91+
spv.func @selection_empty() -> () "None" {
92+
// CHECK: llvm.return
93+
spv.selection {
94+
}
95+
spv.Return
96+
}
97+
98+
spv.func @selection_with_merge_block_only() -> () "None" {
99+
%cond = spv.constant true
100+
// CHECK: llvm.return
101+
spv.selection {
102+
spv.BranchConditional %cond, ^merge, ^merge
103+
^merge:
104+
spv._merge
105+
}
106+
spv.Return
107+
}
108+
109+
spv.func @selection_with_true_block_only() -> () "None" {
110+
// CHECK: %[[COND:.*]] = llvm.mlir.constant(true) : !llvm.i1
111+
%cond = spv.constant true
112+
// CHECK: llvm.cond_br %[[COND]], ^bb1, ^bb2
113+
spv.selection {
114+
spv.BranchConditional %cond, ^true, ^merge
115+
// CHECK: ^bb1:
116+
^true:
117+
// CHECK: llvm.br ^bb2
118+
spv.Branch ^merge
119+
// CHECK: ^bb2:
120+
^merge:
121+
// CHECK: llvm.br ^bb3
122+
spv._merge
123+
}
124+
// CHECK: ^bb3:
125+
// CHECK-NEXT: llvm.return
126+
spv.Return
127+
}
128+
129+
spv.func @selection_with_both_true_and_false_block() -> () "None" {
130+
// CHECK: %[[COND:.*]] = llvm.mlir.constant(true) : !llvm.i1
131+
%cond = spv.constant true
132+
// CHECK: llvm.cond_br %[[COND]], ^bb1, ^bb2
133+
spv.selection {
134+
spv.BranchConditional %cond, ^true, ^false
135+
// CHECK: ^bb1:
136+
^true:
137+
// CHECK: llvm.br ^bb3
138+
spv.Branch ^merge
139+
// CHECK: ^bb2:
140+
^false:
141+
// CHECK: llvm.br ^bb3
142+
spv.Branch ^merge
143+
// CHECK: ^bb3:
144+
^merge:
145+
// CHECK: llvm.br ^bb4
146+
spv._merge
147+
}
148+
// CHECK: ^bb4:
149+
// CHECK-NEXT: llvm.return
150+
spv.Return
151+
}
152+
153+
spv.func @selection_with_early_return(%arg0: i1) -> i32 "None" {
154+
// CHECK: %[[ZERO:.*]] = llvm.mlir.constant(0 : i32) : !llvm.i32
155+
%0 = spv.constant 0 : i32
156+
// CHECK: llvm.cond_br %{{.*}}, ^bb1(%[[ZERO]] : !llvm.i32), ^bb2
157+
spv.selection {
158+
spv.BranchConditional %arg0, ^true(%0 : i32), ^merge
159+
// CHECK: ^bb1(%[[ARG:.*]]: !llvm.i32):
160+
^true(%arg1: i32):
161+
// CHECK: llvm.return %[[ARG]] : !llvm.i32
162+
spv.ReturnValue %arg1 : i32
163+
// CHECK: ^bb2:
164+
^merge:
165+
// CHECK: llvm.br ^bb3
166+
spv._merge
167+
}
168+
// CHECK: ^bb3:
169+
%one = spv.constant 1 : i32
170+
spv.ReturnValue %one : i32
171+
}
172+
}

0 commit comments

Comments
 (0)