Skip to content

Commit 06383c5

Browse files
committed
Merge commit '0496bb4abdd3' into matthias.bump_e5ed7b6e2fd3
2 parents cbc1543 + 0496bb4 commit 06383c5

File tree

39 files changed

+477
-95
lines changed

39 files changed

+477
-95
lines changed

mlir/include/mlir/Dialect/EmitC/IR/EmitC.td

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,36 @@ def EmitC_SubOp : EmitC_BinaryOp<"sub", [CExpression]> {
908908
let hasVerifier = 1;
909909
}
910910

911+
def EmitC_ConditionalOp : EmitC_Op<"conditional",
912+
[AllTypesMatch<["true_value", "false_value", "result"]>, CExpression]> {
913+
let summary = "Conditional (ternary) operation";
914+
let description = [{
915+
With the `conditional` operation the ternary conditional operator can
916+
be applied.
917+
918+
Example:
919+
920+
```mlir
921+
%0 = emitc.cmp gt, %arg0, %arg1 : (i32, i32) -> i1
922+
923+
%c0 = "emitc.constant"() {value = 10 : i32} : () -> i32
924+
%c1 = "emitc.constant"() {value = 11 : i32} : () -> i32
925+
926+
%1 = emitc.conditional %0, %c0, %c1 : i32
927+
```
928+
```c++
929+
// Code emitted for the operations above.
930+
bool v3 = v1 > v2;
931+
int32_t v4 = 10;
932+
int32_t v5 = 11;
933+
int32_t v6 = v3 ? v4 : v5;
934+
```
935+
}];
936+
let arguments = (ins I1:$condition, AnyType:$true_value, AnyType:$false_value);
937+
let results = (outs AnyType:$result);
938+
let assemblyFormat = "operands attr-dict `:` type($result)";
939+
}
940+
911941
def EmitC_UnaryMinusOp : EmitC_UnaryOp<"unary_minus", [CExpression]> {
912942
let summary = "Unary minus operation";
913943
let description = [{

mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ def LLVM_LLVMFuncOp : LLVM_Op<"func", [
14561456
let extraClassDeclaration = [{
14571457
// Add an entry block to an empty function, and set up the block arguments
14581458
// to match the signature of the function.
1459-
Block *addEntryBlock(OpBuilder &builder);
1459+
Block *addEntryBlock();
14601460

14611461
bool isVarArg() { return getFunctionType().isVarArg(); }
14621462

mlir/include/mlir/Dialect/PDL/IR/Builtins.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#ifndef MLIR_DIALECT_PDL_IR_BUILTINS_H_
1414
#define MLIR_DIALECT_PDL_IR_BUILTINS_H_
1515

16+
#include "mlir/IR/PatternMatch.h"
17+
#include "mlir/Support/LogicalResult.h"
18+
#include "llvm/ADT/ArrayRef.h"
19+
1620
namespace mlir {
1721
class PDLPatternModule;
1822
class Attribute;
@@ -29,6 +33,8 @@ Attribute addEntryToDictionaryAttr(PatternRewriter &rewriter,
2933
Attribute createArrayAttr(PatternRewriter &rewriter);
3034
Attribute addElemToArrayAttr(PatternRewriter &rewriter, Attribute attr,
3135
Attribute element);
36+
LogicalResult add(PatternRewriter &rewriter, PDLResultList &results,
37+
llvm::ArrayRef<PDLValue> args);
3238
} // namespace builtin
3339
} // namespace pdl
3440
} // namespace mlir

mlir/include/mlir/Dialect/SPIRV/IR/SPIRVControlFlowOps.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def SPIRV_LoopOp : SPIRV_Op<"mlir.loop", [InFunctionScope]> {
285285

286286
// Adds an empty entry block and loop merge block containing one
287287
// spirv.mlir.merge op.
288-
void addEntryAndMergeBlock(OpBuilder &builder);
288+
void addEntryAndMergeBlock();
289289
}];
290290

291291
let hasOpcode = 0;
@@ -427,7 +427,7 @@ def SPIRV_SelectionOp : SPIRV_Op<"mlir.selection", [InFunctionScope]> {
427427
Block *getMergeBlock();
428428

429429
/// Adds a selection merge block containing one spirv.mlir.merge op.
430-
void addMergeBlock(OpBuilder &builder);
430+
void addMergeBlock();
431431

432432
/// Creates a spirv.mlir.selection op for `if (<condition>) then { <thenBody> }`
433433
/// with `builder`. `builder`'s insertion point will remain at after the

mlir/include/mlir/Interfaces/FunctionInterfaces.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ def FunctionOpInterface : OpInterface<"FunctionOpInterface", [
131131
static void buildWithEntryBlock(
132132
OpBuilder &builder, OperationState &state, StringRef name, Type type,
133133
ArrayRef<NamedAttribute> attrs, TypeRange inputTypes) {
134-
OpBuilder::InsertionGuard g(builder);
135134
state.addAttribute(SymbolTable::getSymbolAttrName(),
136135
builder.getStringAttr(name));
137136
state.addAttribute(ConcreteOp::getFunctionTypeAttrName(state.name),
@@ -140,7 +139,8 @@ def FunctionOpInterface : OpInterface<"FunctionOpInterface", [
140139

141140
// Add the function body.
142141
Region *bodyRegion = state.addRegion();
143-
Block *body = builder.createBlock(bodyRegion);
142+
Block *body = new Block();
143+
bodyRegion->push_back(body);
144144
for (Type input : inputTypes)
145145
body->addArgument(input, state.location);
146146
}

mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,31 @@ class ArithOpConversion final : public OpConversionPattern<ArithOp> {
5454
return success();
5555
}
5656
};
57+
58+
class SelectOpConversion : public OpConversionPattern<arith::SelectOp> {
59+
public:
60+
using OpConversionPattern<arith::SelectOp>::OpConversionPattern;
61+
62+
LogicalResult
63+
matchAndRewrite(arith::SelectOp selectOp, OpAdaptor adaptor,
64+
ConversionPatternRewriter &rewriter) const override {
65+
66+
Type dstType = getTypeConverter()->convertType(selectOp.getType());
67+
if (!dstType)
68+
return rewriter.notifyMatchFailure(selectOp, "type conversion failed");
69+
70+
if (!adaptor.getCondition().getType().isInteger(1))
71+
return rewriter.notifyMatchFailure(
72+
selectOp,
73+
"can only be converted if condition is a scalar of type i1");
74+
75+
rewriter.replaceOpWithNewOp<emitc::ConditionalOp>(selectOp, dstType,
76+
adaptor.getOperands());
77+
78+
return success();
79+
}
80+
};
81+
5782
} // namespace
5883

5984
//===----------------------------------------------------------------------===//
@@ -70,7 +95,8 @@ void mlir::populateArithToEmitCPatterns(TypeConverter &typeConverter,
7095
ArithOpConversion<arith::AddFOp, emitc::AddOp>,
7196
ArithOpConversion<arith::DivFOp, emitc::DivOp>,
7297
ArithOpConversion<arith::MulFOp, emitc::MulOp>,
73-
ArithOpConversion<arith::SubFOp, emitc::SubOp>
98+
ArithOpConversion<arith::SubFOp, emitc::SubOp>,
99+
SelectOpConversion
74100
>(typeConverter, ctx);
75101
// clang-format on
76102
}

mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ static void addResumeFunction(ModuleOp module) {
259259
kResume, LLVM::LLVMFunctionType::get(voidTy, {ptrType}));
260260
resumeOp.setPrivate();
261261

262-
auto *block = resumeOp.addEntryBlock(moduleBuilder);
262+
auto *block = resumeOp.addEntryBlock();
263263
auto blockBuilder = ImplicitLocOpBuilder::atBlockEnd(loc, block);
264264

265265
blockBuilder.create<LLVM::CoroResumeOp>(resumeOp.getArgument(0));

mlir/lib/Conversion/ControlFlowToSCF/ControlFlowToSCF.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ ControlFlowToSCFTransformation::createStructuredDoWhileLoopOp(
9898
loc, builder.create<arith::TruncIOp>(loc, builder.getI1Type(), condition),
9999
loopVariablesNextIter);
100100

101-
Block *afterBlock = builder.createBlock(&whileOp.getAfter());
101+
auto *afterBlock = new Block;
102+
whileOp.getAfter().push_back(afterBlock);
102103
afterBlock->addArguments(
103104
loopVariablesInit.getTypes(),
104105
SmallVector<Location>(loopVariablesInit.size(), loc));
106+
builder.setInsertionPointToEnd(afterBlock);
105107
builder.create<scf::YieldOp>(loc, afterBlock->getArguments());
106108

107109
return whileOp.getOperation();

mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static void wrapForExternalCallers(OpBuilder &rewriter, Location loc,
135135
propagateArgResAttrs(rewriter, !!resultStructType, funcOp, wrapperFuncOp);
136136

137137
OpBuilder::InsertionGuard guard(rewriter);
138-
rewriter.setInsertionPointToStart(wrapperFuncOp.addEntryBlock(rewriter));
138+
rewriter.setInsertionPointToStart(wrapperFuncOp.addEntryBlock());
139139

140140
SmallVector<Value, 8> args;
141141
size_t argOffset = resultStructType ? 1 : 0;
@@ -203,7 +203,7 @@ static void wrapExternalFunction(OpBuilder &builder, Location loc,
203203

204204
// The wrapper that we synthetize here should only be visible in this module.
205205
newFuncOp.setLinkage(LLVM::Linkage::Private);
206-
builder.setInsertionPointToStart(newFuncOp.addEntryBlock(builder));
206+
builder.setInsertionPointToStart(newFuncOp.addEntryBlock());
207207

208208
// Get a ValueRange containing arguments.
209209
FunctionType type = cast<FunctionType>(funcOp.getFunctionType());

mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,9 @@ struct GlobalMemrefOpLowering
520520
global, arrayTy, global.getConstant(), linkage, global.getSymName(),
521521
initialValue, alignment, *addressSpace);
522522
if (!global.isExternal() && global.isUninitialized()) {
523-
rewriter.createBlock(&newGlobal.getInitializerRegion());
523+
Block *blk = new Block();
524+
newGlobal.getInitializerRegion().push_back(blk);
525+
rewriter.setInsertionPointToStart(blk);
524526
Value undef[] = {
525527
rewriter.create<LLVM::UndefOp>(global.getLoc(), arrayTy)};
526528
rewriter.create<LLVM::ReturnOp>(global.getLoc(), undef);

mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,14 @@ struct ForOpConversion final : SCFToSPIRVPattern<scf::ForOp> {
138138
// from header to merge.
139139
auto loc = forOp.getLoc();
140140
auto loopOp = rewriter.create<spirv::LoopOp>(loc, spirv::LoopControl::None);
141-
loopOp.addEntryAndMergeBlock(rewriter);
141+
loopOp.addEntryAndMergeBlock();
142142

143143
OpBuilder::InsertionGuard guard(rewriter);
144144
// Create the block for the header.
145-
Block *header = rewriter.createBlock(&loopOp.getBody(),
146-
getBlockIt(loopOp.getBody(), 1));
147-
rewriter.setInsertionPointAfter(loopOp);
145+
auto *header = new Block();
146+
// Insert the header.
147+
loopOp.getBody().getBlocks().insert(getBlockIt(loopOp.getBody(), 1),
148+
header);
148149

149150
// Create the new induction variable to use.
150151
Value adapLowerBound = adaptor.getLowerBound();
@@ -341,7 +342,7 @@ struct WhileOpConversion final : SCFToSPIRVPattern<scf::WhileOp> {
341342
ConversionPatternRewriter &rewriter) const override {
342343
auto loc = whileOp.getLoc();
343344
auto loopOp = rewriter.create<spirv::LoopOp>(loc, spirv::LoopControl::None);
344-
loopOp.addEntryAndMergeBlock(rewriter);
345+
loopOp.addEntryAndMergeBlock();
345346

346347
Region &beforeRegion = whileOp.getBefore();
347348
Region &afterRegion = whileOp.getAfter();

mlir/lib/Dialect/Affine/IR/AffineOps.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,8 +1813,6 @@ void AffineForOp::build(OpBuilder &builder, OperationState &result,
18131813
"upper bound operand count does not match the affine map");
18141814
assert(step > 0 && "step has to be a positive integer constant");
18151815

1816-
OpBuilder::InsertionGuard guard(builder);
1817-
18181816
// Set variadic segment sizes.
18191817
result.addAttribute(
18201818
getOperandSegmentSizeAttr(),
@@ -1843,11 +1841,12 @@ void AffineForOp::build(OpBuilder &builder, OperationState &result,
18431841
// Create a region and a block for the body. The argument of the region is
18441842
// the loop induction variable.
18451843
Region *bodyRegion = result.addRegion();
1846-
Block *bodyBlock = builder.createBlock(bodyRegion);
1844+
bodyRegion->push_back(new Block);
1845+
Block &bodyBlock = bodyRegion->front();
18471846
Value inductionVar =
1848-
bodyBlock->addArgument(builder.getIndexType(), result.location);
1847+
bodyBlock.addArgument(builder.getIndexType(), result.location);
18491848
for (Value val : iterArgs)
1850-
bodyBlock->addArgument(val.getType(), val.getLoc());
1849+
bodyBlock.addArgument(val.getType(), val.getLoc());
18511850

18521851
// Create the default terminator if the builder is not provided and if the
18531852
// iteration arguments are not provided. Otherwise, leave this to the caller
@@ -1856,9 +1855,9 @@ void AffineForOp::build(OpBuilder &builder, OperationState &result,
18561855
ensureTerminator(*bodyRegion, builder, result.location);
18571856
} else if (bodyBuilder) {
18581857
OpBuilder::InsertionGuard guard(builder);
1859-
builder.setInsertionPointToStart(bodyBlock);
1858+
builder.setInsertionPointToStart(&bodyBlock);
18601859
bodyBuilder(builder, result.location, inductionVar,
1861-
bodyBlock->getArguments().drop_front());
1860+
bodyBlock.getArguments().drop_front());
18621861
}
18631862
}
18641863

@@ -2896,20 +2895,18 @@ void AffineIfOp::build(OpBuilder &builder, OperationState &result,
28962895
TypeRange resultTypes, IntegerSet set, ValueRange args,
28972896
bool withElseRegion) {
28982897
assert(resultTypes.empty() || withElseRegion);
2899-
OpBuilder::InsertionGuard guard(builder);
2900-
29012898
result.addTypes(resultTypes);
29022899
result.addOperands(args);
29032900
result.addAttribute(getConditionAttrStrName(), IntegerSetAttr::get(set));
29042901

29052902
Region *thenRegion = result.addRegion();
2906-
builder.createBlock(thenRegion);
2903+
thenRegion->push_back(new Block());
29072904
if (resultTypes.empty())
29082905
AffineIfOp::ensureTerminator(*thenRegion, builder, result.location);
29092906

29102907
Region *elseRegion = result.addRegion();
29112908
if (withElseRegion) {
2912-
builder.createBlock(elseRegion);
2909+
elseRegion->push_back(new Block());
29132910
if (resultTypes.empty())
29142911
AffineIfOp::ensureTerminator(*elseRegion, builder, result.location);
29152912
}
@@ -3696,7 +3693,6 @@ void AffineParallelOp::build(OpBuilder &builder, OperationState &result,
36963693
"expected upper bound maps to have as many inputs as upper bound "
36973694
"operands");
36983695

3699-
OpBuilder::InsertionGuard guard(builder);
37003696
result.addTypes(resultTypes);
37013697

37023698
// Convert the reductions to integer attributes.
@@ -3742,11 +3738,11 @@ void AffineParallelOp::build(OpBuilder &builder, OperationState &result,
37423738

37433739
// Create a region and a block for the body.
37443740
auto *bodyRegion = result.addRegion();
3745-
Block *body = builder.createBlock(bodyRegion);
3746-
3741+
auto *body = new Block();
37473742
// Add all the block arguments.
37483743
for (unsigned i = 0, e = steps.size(); i < e; ++i)
37493744
body->addArgument(IndexType::get(builder.getContext()), result.location);
3745+
bodyRegion->push_back(body);
37503746
if (resultTypes.empty())
37513747
ensureTerminator(*bodyRegion, builder, result.location);
37523748
}

mlir/lib/Dialect/Async/IR/Async.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void ExecuteOp::getSuccessorRegions(RegionBranchPoint point,
6868
void ExecuteOp::build(OpBuilder &builder, OperationState &result,
6969
TypeRange resultTypes, ValueRange dependencies,
7070
ValueRange operands, BodyBuilderFn bodyBuilder) {
71-
OpBuilder::InsertionGuard guard(builder);
71+
7272
result.addOperands(dependencies);
7373
result.addOperands(operands);
7474

@@ -87,21 +87,26 @@ void ExecuteOp::build(OpBuilder &builder, OperationState &result,
8787

8888
// Add a body region with block arguments as unwrapped async value operands.
8989
Region *bodyRegion = result.addRegion();
90-
Block *bodyBlock = builder.createBlock(bodyRegion);
90+
bodyRegion->push_back(new Block);
91+
Block &bodyBlock = bodyRegion->front();
9192
for (Value operand : operands) {
9293
auto valueType = llvm::dyn_cast<ValueType>(operand.getType());
93-
bodyBlock->addArgument(valueType ? valueType.getValueType()
94-
: operand.getType(),
95-
operand.getLoc());
94+
bodyBlock.addArgument(valueType ? valueType.getValueType()
95+
: operand.getType(),
96+
operand.getLoc());
9697
}
9798

9899
// Create the default terminator if the builder is not provided and if the
99100
// expected result is empty. Otherwise, leave this to the caller
100101
// because we don't know which values to return from the execute op.
101102
if (resultTypes.empty() && !bodyBuilder) {
103+
OpBuilder::InsertionGuard guard(builder);
104+
builder.setInsertionPointToStart(&bodyBlock);
102105
builder.create<async::YieldOp>(result.location, ValueRange());
103106
} else if (bodyBuilder) {
104-
bodyBuilder(builder, result.location, bodyBlock->getArguments());
107+
OpBuilder::InsertionGuard guard(builder);
108+
builder.setInsertionPointToStart(&bodyBlock);
109+
bodyBuilder(builder, result.location, bodyBlock.getArguments());
105110
}
106111
}
107112

mlir/lib/Dialect/EmitC/IR/EmitC.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,20 +270,20 @@ LogicalResult ExpressionOp::verify() {
270270

271271
void ForOp::build(OpBuilder &builder, OperationState &result, Value lb,
272272
Value ub, Value step, BodyBuilderFn bodyBuilder) {
273-
OpBuilder::InsertionGuard g(builder);
274273
result.addOperands({lb, ub, step});
275274
Type t = lb.getType();
276275
Region *bodyRegion = result.addRegion();
277-
Block *bodyBlock = builder.createBlock(bodyRegion);
278-
bodyBlock->addArgument(t, result.location);
276+
bodyRegion->push_back(new Block);
277+
Block &bodyBlock = bodyRegion->front();
278+
bodyBlock.addArgument(t, result.location);
279279

280280
// Create the default terminator if the builder is not provided.
281281
if (!bodyBuilder) {
282282
ForOp::ensureTerminator(*bodyRegion, builder, result.location);
283283
} else {
284284
OpBuilder::InsertionGuard guard(builder);
285-
builder.setInsertionPointToStart(bodyBlock);
286-
bodyBuilder(builder, result.location, bodyBlock->getArgument(0));
285+
builder.setInsertionPointToStart(&bodyBlock);
286+
bodyBuilder(builder, result.location, bodyBlock.getArgument(0));
287287
}
288288
}
289289

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,10 +2165,11 @@ LogicalResult ShuffleVectorOp::verify() {
21652165
//===----------------------------------------------------------------------===//
21662166

21672167
// Add the entry block to the function.
2168-
Block *LLVMFuncOp::addEntryBlock(OpBuilder &builder) {
2168+
Block *LLVMFuncOp::addEntryBlock() {
21692169
assert(empty() && "function already has an entry block");
2170-
OpBuilder::InsertionGuard g(builder);
2171-
Block *entry = builder.createBlock(&getBody());
2170+
2171+
auto *entry = new Block;
2172+
push_back(entry);
21722173

21732174
// FIXME: Allow passing in proper locations for the entry arguments.
21742175
LLVMFunctionType type = getFunctionType();

0 commit comments

Comments
 (0)