Skip to content

Commit e67080d

Browse files
authored
[mlir][ods] Populate properties in generated builder (#90430)
Previously this was only populated in the create method later. This resolves some of invalid builder paths. This may also be sufficient that type inference functions no longer have to consider whether property conversion has happened (but haven't verified that yet). This also makes Attributes corresponding to Properties as optional inside the set from attributes method. Today that is in effect what happens with Property value initialization and folks use it to define custom C++ types whose default initialization is what they want. This is the behavior users get if they use properties directly. Propagating Attributes without allowing partial setting would require iterating over the dictionary attribute considering the properties of the op type that will be created. This could also have been an additional method generated or optional behavior on the set method. But doing it consistently seems better. In terms of whats lost, it doesn't seem like anything compared to the pure Property path where Property is default value initialized and then partially overwritten (this doesn't seem to buy anything else verification wise). Default valued Properties (as specified ODS side rather than C++ side) triggered error as the containing class was not yet complete but referenced nested class, so that we couldn't have default initializer for them in the parent class. Added an additional forwarding builder to avoid needing to update call sites. This could be split out to separate change. Inlined templated function in unit test that was only used once. Moved initialization earlier where seen.
1 parent 03bdfb6 commit e67080d

File tree

12 files changed

+207
-75
lines changed

12 files changed

+207
-75
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ class LLVM_IntArithmeticOpWithOverflowFlag<string mnemonic, string instName,
6565
let builders = [
6666
OpBuilder<(ins "Type":$type, "Value":$lhs, "Value":$rhs,
6767
"IntegerOverflowFlags":$overflowFlags), [{
68-
build($_builder, $_state, type, lhs, rhs);
6968
$_state.getOrAddProperties<Properties>().overflowFlags = overflowFlags;
69+
build($_builder, $_state, type, lhs, rhs);
7070
}]>,
7171
OpBuilder<(ins "Value":$lhs, "Value":$rhs,
7272
"IntegerOverflowFlags":$overflowFlags), [{
73-
build($_builder, $_state, lhs, rhs);
7473
$_state.getOrAddProperties<Properties>().overflowFlags = overflowFlags;
74+
build($_builder, $_state, lhs, rhs);
7575
}]>
7676
];
7777

mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1764,9 +1764,9 @@ def MemRef_CollapseShapeOp : MemRef_ReassociativeReshapeOp<"collapse_shape", [
17641764
"ArrayRef<ReassociationIndices>":$reassociation,
17651765
CArg<"ArrayRef<NamedAttribute>", "{}">:$attrs),
17661766
[{
1767-
build($_builder, $_state, resultType, src, attrs);
17681767
$_state.addAttribute("reassociation",
17691768
getReassociationIndicesAttribute($_builder, reassociation));
1769+
build($_builder, $_state, resultType, src, attrs);
17701770
}]>,
17711771
OpBuilder<(ins "Type":$resultType, "Value":$src,
17721772
"ArrayRef<ReassociationExprs>":$reassociation,

mlir/include/mlir/Dialect/Tensor/IR/TensorOps.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1216,9 +1216,9 @@ def Tensor_CollapseShapeOp : Tensor_ReassociativeReshapeOp<"collapse_shape"> {
12161216
"ArrayRef<ReassociationIndices>":$reassociation,
12171217
CArg<"ArrayRef<NamedAttribute>", "{}">:$attrs),
12181218
[{
1219-
build($_builder, $_state, resultType, src, attrs);
12201219
$_state.addAttribute("reassociation",
12211220
getReassociationIndicesAttribute($_builder, reassociation));
1221+
build($_builder, $_state, resultType, src, attrs);
12221222
}]>,
12231223
OpBuilder<(ins "Type":$resultType, "Value":$src,
12241224
"ArrayRef<ReassociationExprs>":$reassociation,

mlir/include/mlir/IR/Operation.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,11 +916,12 @@ class alignas(8) Operation final
916916
/// operation. Returns an empty attribute if no properties are present.
917917
Attribute getPropertiesAsAttribute();
918918

919-
/// Set the properties from the provided attribute.
919+
/// Set the properties from the provided attribute.
920920
/// This is an expensive operation that can fail if the attribute is not
921921
/// matching the expectations of the properties for this operation. This is
922922
/// mostly useful for unregistered operations or used when parsing the
923-
/// generic format. An optional diagnostic can be passed in for richer errors.
923+
/// generic format. An optional diagnostic emitter can be passed in for richer
924+
/// errors, if none is passed then behavior is undefined in error case.
924925
LogicalResult
925926
setPropertiesFromAttribute(Attribute attr,
926927
function_ref<InFlightDiagnostic()> emitError);

mlir/lib/Dialect/Func/Transforms/OneToNFuncConversions.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class ConvertTypesInFuncCallOp : public OneToNOpConversionPattern<CallOp> {
4040
return failure();
4141

4242
// Create new CallOp.
43-
auto newOp = rewriter.create<CallOp>(loc, resultMapping.getConvertedTypes(),
44-
adaptor.getFlatOperands());
45-
newOp->setAttrs(op->getAttrs());
43+
auto newOp =
44+
rewriter.create<CallOp>(loc, resultMapping.getConvertedTypes(),
45+
adaptor.getFlatOperands(), op->getAttrs());
4646

4747
rewriter.replaceOp(op, newOp->getResults(), resultMapping);
4848
return success();

mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,11 +1806,11 @@ void ReinterpretCastOp::build(OpBuilder &b, OperationState &result,
18061806
dispatchIndexOpFoldResults(offset, dynamicOffsets, staticOffsets);
18071807
dispatchIndexOpFoldResults(sizes, dynamicSizes, staticSizes);
18081808
dispatchIndexOpFoldResults(strides, dynamicStrides, staticStrides);
1809+
result.addAttributes(attrs);
18091810
build(b, result, resultType, source, dynamicOffsets, dynamicSizes,
18101811
dynamicStrides, b.getDenseI64ArrayAttr(staticOffsets),
18111812
b.getDenseI64ArrayAttr(staticSizes),
18121813
b.getDenseI64ArrayAttr(staticStrides));
1813-
result.addAttributes(attrs);
18141814
}
18151815

18161816
void ReinterpretCastOp::build(OpBuilder &b, OperationState &result,
@@ -2483,9 +2483,9 @@ void CollapseShapeOp::build(OpBuilder &b, OperationState &result, Value src,
24832483
auto srcType = llvm::cast<MemRefType>(src.getType());
24842484
MemRefType resultType =
24852485
CollapseShapeOp::computeCollapsedType(srcType, reassociation);
2486-
build(b, result, resultType, src, attrs);
24872486
result.addAttribute(::mlir::getReassociationAttrName(),
24882487
getReassociationIndicesAttribute(b, reassociation));
2488+
build(b, result, resultType, src, attrs);
24892489
}
24902490

24912491
LogicalResult CollapseShapeOp::verify() {
@@ -2781,11 +2781,11 @@ void SubViewOp::build(OpBuilder &b, OperationState &result,
27812781
resultType = llvm::cast<MemRefType>(SubViewOp::inferResultType(
27822782
sourceMemRefType, staticOffsets, staticSizes, staticStrides));
27832783
}
2784+
result.addAttributes(attrs);
27842785
build(b, result, resultType, source, dynamicOffsets, dynamicSizes,
27852786
dynamicStrides, b.getDenseI64ArrayAttr(staticOffsets),
27862787
b.getDenseI64ArrayAttr(staticSizes),
27872788
b.getDenseI64ArrayAttr(staticStrides));
2788-
result.addAttributes(attrs);
27892789
}
27902790

27912791
// Build a SubViewOp with mixed static and dynamic entries and inferred result
@@ -3320,8 +3320,8 @@ void TransposeOp::build(OpBuilder &b, OperationState &result, Value in,
33203320
// Compute result type.
33213321
MemRefType resultType = inferTransposeResultType(memRefType, permutationMap);
33223322

3323-
build(b, result, resultType, in, attrs);
33243323
result.addAttribute(TransposeOp::getPermutationAttrStrName(), permutation);
3324+
build(b, result, resultType, in, attrs);
33253325
}
33263326

33273327
// transpose $in $permutation attr-dict : type($in) `to` type(results)

mlir/lib/Dialect/Tensor/IR/TensorOps.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,9 +1743,9 @@ void CollapseShapeOp::build(OpBuilder &b, OperationState &result, Value src,
17431743
llvm::cast<RankedTensorType>(src.getType()),
17441744
getSymbolLessAffineMaps(
17451745
convertReassociationIndicesToExprs(b.getContext(), reassociation)));
1746-
build(b, result, resultType, src, attrs);
17471746
result.addAttribute(getReassociationAttrStrName(),
17481747
getReassociationIndicesAttribute(b, reassociation));
1748+
build(b, result, resultType, src, attrs);
17491749
}
17501750

17511751
template <typename TensorReshapeOp, bool isExpansion = std::is_same<
@@ -2100,11 +2100,11 @@ void ExtractSliceOp::build(OpBuilder &b, OperationState &result,
21002100
resultType = llvm::cast<RankedTensorType>(ExtractSliceOp::inferResultType(
21012101
sourceRankedTensorType, staticOffsets, staticSizes, staticStrides));
21022102
}
2103+
result.addAttributes(attrs);
21032104
build(b, result, resultType, source, dynamicOffsets, dynamicSizes,
21042105
dynamicStrides, b.getDenseI64ArrayAttr(staticOffsets),
21052106
b.getDenseI64ArrayAttr(staticSizes),
21062107
b.getDenseI64ArrayAttr(staticStrides));
2107-
result.addAttributes(attrs);
21082108
}
21092109

21102110
/// Build an ExtractSliceOp with mixed static and dynamic entries and inferred
@@ -2499,11 +2499,11 @@ void InsertSliceOp::build(OpBuilder &b, OperationState &result, Value source,
24992499
dispatchIndexOpFoldResults(offsets, dynamicOffsets, staticOffsets);
25002500
dispatchIndexOpFoldResults(sizes, dynamicSizes, staticSizes);
25012501
dispatchIndexOpFoldResults(strides, dynamicStrides, staticStrides);
2502+
result.addAttributes(attrs);
25022503
build(b, result, dest.getType(), source, dest, dynamicOffsets, dynamicSizes,
25032504
dynamicStrides, b.getDenseI64ArrayAttr(staticOffsets),
25042505
b.getDenseI64ArrayAttr(staticSizes),
25052506
b.getDenseI64ArrayAttr(staticStrides));
2506-
result.addAttributes(attrs);
25072507
}
25082508

25092509
/// Build an InsertSliceOp with mixed static and dynamic entries packed into a
@@ -2967,10 +2967,10 @@ void PadOp::build(OpBuilder &b, OperationState &result, Type resultType,
29672967
auto sourceType = llvm::cast<RankedTensorType>(source.getType());
29682968
if (!resultType)
29692969
resultType = inferResultType(sourceType, staticLow, staticHigh);
2970+
result.addAttributes(attrs);
29702971
build(b, result, resultType, source, low, high,
29712972
b.getDenseI64ArrayAttr(staticLow), b.getDenseI64ArrayAttr(staticHigh),
29722973
nofold ? b.getUnitAttr() : UnitAttr());
2973-
result.addAttributes(attrs);
29742974
}
29752975

29762976
void PadOp::build(OpBuilder &b, OperationState &result, Type resultType,
@@ -3000,10 +3000,10 @@ void PadOp::build(OpBuilder &b, OperationState &result, Type resultType,
30003000
resultType = PadOp::inferResultType(sourceType, staticLow, staticHigh);
30013001
}
30023002
assert(llvm::isa<RankedTensorType>(resultType));
3003+
result.addAttributes(attrs);
30033004
build(b, result, resultType, source, dynamicLow, dynamicHigh,
30043005
b.getDenseI64ArrayAttr(staticLow), b.getDenseI64ArrayAttr(staticHigh),
30053006
nofold ? b.getUnitAttr() : UnitAttr());
3006-
result.addAttributes(attrs);
30073007
}
30083008

30093009
void PadOp::build(OpBuilder &b, OperationState &result, Type resultType,
@@ -3447,11 +3447,11 @@ void ParallelInsertSliceOp::build(OpBuilder &b, OperationState &result,
34473447
dispatchIndexOpFoldResults(offsets, dynamicOffsets, staticOffsets);
34483448
dispatchIndexOpFoldResults(sizes, dynamicSizes, staticSizes);
34493449
dispatchIndexOpFoldResults(strides, dynamicStrides, staticStrides);
3450+
result.addAttributes(attrs);
34503451
build(b, result, {}, source, dest, dynamicOffsets, dynamicSizes,
34513452
dynamicStrides, b.getDenseI64ArrayAttr(staticOffsets),
34523453
b.getDenseI64ArrayAttr(staticSizes),
34533454
b.getDenseI64ArrayAttr(staticStrides));
3454-
result.addAttributes(attrs);
34553455
}
34563456

34573457
/// Build an ParallelInsertSliceOp with mixed static and dynamic entries

mlir/test/Dialect/OpenMP/invalid.mlir

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,23 +2113,23 @@ func.func @omp_distribute_allocate(%data_var : memref<i32>) -> () {
21132113

21142114
func.func @omp_distribute_wrapper() -> () {
21152115
// expected-error @below {{op must be a loop wrapper}}
2116-
"omp.distribute"() ({
2116+
omp.distribute {
21172117
%0 = arith.constant 0 : i32
21182118
"omp.terminator"() : () -> ()
2119-
}) : () -> ()
2119+
}
21202120
}
21212121

21222122
// -----
21232123

21242124
func.func @omp_distribute_nested_wrapper(%data_var : memref<i32>) -> () {
21252125
// expected-error @below {{only supported nested wrappers are 'omp.parallel' and 'omp.simd'}}
2126-
"omp.distribute"() ({
2127-
"omp.wsloop"() ({
2128-
%0 = arith.constant 0 : i32
2129-
"omp.terminator"() : () -> ()
2130-
}) : () -> ()
2126+
omp.distribute {
2127+
"omp.wsloop"() ({
2128+
%0 = arith.constant 0 : i32
21312129
"omp.terminator"() : () -> ()
21322130
}) : () -> ()
2131+
"omp.terminator"() : () -> ()
2132+
}
21332133
}
21342134

21352135
// -----

mlir/test/Dialect/OpenMP/ops.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,13 +521,13 @@ func.func @omp_wsloop_pretty(%lb : index, %ub : index, %step : index, %data_var
521521
// CHECK-LABEL: omp_simd
522522
func.func @omp_simd(%lb : index, %ub : index, %step : index) -> () {
523523
// CHECK: omp.simd
524-
"omp.simd" () ({
524+
omp.simd {
525525
"omp.loop_nest" (%lb, %ub, %step) ({
526526
^bb1(%iv2: index):
527527
"omp.yield"() : () -> ()
528528
}) : (index, index, index) -> ()
529529
"omp.terminator"() : () -> ()
530-
}) : () -> ()
530+
}
531531

532532
return
533533
}

mlir/test/lib/Dialect/Test/TestOps.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,6 +2402,13 @@ def TableGenBuildOp5 : TableGenBuildInferReturnTypeBaseOp<
24022402
let regions = (region AnyRegion:$body);
24032403
}
24042404

2405+
// Two variadic args, non variadic results, with AttrSizedOperandSegments
2406+
// Test build method generation for property conversion & type inference.
2407+
def TableGenBuildOp6 : TEST_Op<"tblgen_build_6", [AttrSizedOperandSegments]> {
2408+
let arguments = (ins Variadic<AnyType>:$a, Variadic<AnyType>:$b);
2409+
let results = (outs F32:$result);
2410+
}
2411+
24052412
//===----------------------------------------------------------------------===//
24062413
// Test BufferPlacement
24072414
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)