Skip to content

Commit df1c7c0

Browse files
committed
[mlir][ods] Populate properties in generated builder
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 1x. Moved initialization earlier where seen.
1 parent 3a83162 commit df1c7c0

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
@@ -1808,11 +1808,11 @@ void ReinterpretCastOp::build(OpBuilder &b, OperationState &result,
18081808
dispatchIndexOpFoldResults(offset, dynamicOffsets, staticOffsets);
18091809
dispatchIndexOpFoldResults(sizes, dynamicSizes, staticSizes);
18101810
dispatchIndexOpFoldResults(strides, dynamicStrides, staticStrides);
1811+
result.addAttributes(attrs);
18111812
build(b, result, resultType, source, dynamicOffsets, dynamicSizes,
18121813
dynamicStrides, b.getDenseI64ArrayAttr(staticOffsets),
18131814
b.getDenseI64ArrayAttr(staticSizes),
18141815
b.getDenseI64ArrayAttr(staticStrides));
1815-
result.addAttributes(attrs);
18161816
}
18171817

18181818
void ReinterpretCastOp::build(OpBuilder &b, OperationState &result,
@@ -2486,9 +2486,9 @@ void CollapseShapeOp::build(OpBuilder &b, OperationState &result, Value src,
24862486
auto srcType = llvm::cast<MemRefType>(src.getType());
24872487
MemRefType resultType =
24882488
CollapseShapeOp::computeCollapsedType(srcType, reassociation);
2489-
build(b, result, resultType, src, attrs);
24902489
result.addAttribute(::mlir::getReassociationAttrName(),
24912490
getReassociationIndicesAttribute(b, reassociation));
2491+
build(b, result, resultType, src, attrs);
24922492
}
24932493

24942494
LogicalResult CollapseShapeOp::verify() {
@@ -2784,11 +2784,11 @@ void SubViewOp::build(OpBuilder &b, OperationState &result,
27842784
resultType = llvm::cast<MemRefType>(SubViewOp::inferResultType(
27852785
sourceMemRefType, staticOffsets, staticSizes, staticStrides));
27862786
}
2787+
result.addAttributes(attrs);
27872788
build(b, result, resultType, source, dynamicOffsets, dynamicSizes,
27882789
dynamicStrides, b.getDenseI64ArrayAttr(staticOffsets),
27892790
b.getDenseI64ArrayAttr(staticSizes),
27902791
b.getDenseI64ArrayAttr(staticStrides));
2791-
result.addAttributes(attrs);
27922792
}
27932793

27942794
// Build a SubViewOp with mixed static and dynamic entries and inferred result
@@ -3323,8 +3323,8 @@ void TransposeOp::build(OpBuilder &b, OperationState &result, Value in,
33233323
// Compute result type.
33243324
MemRefType resultType = inferTransposeResultType(memRefType, permutationMap);
33253325

3326-
build(b, result, resultType, in, attrs);
33273326
result.addAttribute(TransposeOp::getPermutationAttrStrName(), permutation);
3327+
build(b, result, resultType, in, attrs);
33283328
}
33293329

33303330
// 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<
@@ -2099,11 +2099,11 @@ void ExtractSliceOp::build(OpBuilder &b, OperationState &result,
20992099
resultType = llvm::cast<RankedTensorType>(ExtractSliceOp::inferResultType(
21002100
sourceRankedTensorType, staticOffsets, staticSizes, staticStrides));
21012101
}
2102+
result.addAttributes(attrs);
21022103
build(b, result, resultType, source, dynamicOffsets, dynamicSizes,
21032104
dynamicStrides, b.getDenseI64ArrayAttr(staticOffsets),
21042105
b.getDenseI64ArrayAttr(staticSizes),
21052106
b.getDenseI64ArrayAttr(staticStrides));
2106-
result.addAttributes(attrs);
21072107
}
21082108

21092109
/// Build an ExtractSliceOp with mixed static and dynamic entries and inferred
@@ -2498,11 +2498,11 @@ void InsertSliceOp::build(OpBuilder &b, OperationState &result, Value source,
24982498
dispatchIndexOpFoldResults(offsets, dynamicOffsets, staticOffsets);
24992499
dispatchIndexOpFoldResults(sizes, dynamicSizes, staticSizes);
25002500
dispatchIndexOpFoldResults(strides, dynamicStrides, staticStrides);
2501+
result.addAttributes(attrs);
25012502
build(b, result, dest.getType(), source, dest, dynamicOffsets, dynamicSizes,
25022503
dynamicStrides, b.getDenseI64ArrayAttr(staticOffsets),
25032504
b.getDenseI64ArrayAttr(staticSizes),
25042505
b.getDenseI64ArrayAttr(staticStrides));
2505-
result.addAttributes(attrs);
25062506
}
25072507

25082508
/// Build an InsertSliceOp with mixed static and dynamic entries packed into a
@@ -2943,10 +2943,10 @@ void PadOp::build(OpBuilder &b, OperationState &result, Type resultType,
29432943
auto sourceType = llvm::cast<RankedTensorType>(source.getType());
29442944
if (!resultType)
29452945
resultType = inferResultType(sourceType, staticLow, staticHigh);
2946+
result.addAttributes(attrs);
29462947
build(b, result, resultType, source, low, high,
29472948
b.getDenseI64ArrayAttr(staticLow), b.getDenseI64ArrayAttr(staticHigh),
29482949
nofold ? b.getUnitAttr() : UnitAttr());
2949-
result.addAttributes(attrs);
29502950
}
29512951

29522952
void PadOp::build(OpBuilder &b, OperationState &result, Type resultType,
@@ -2976,10 +2976,10 @@ void PadOp::build(OpBuilder &b, OperationState &result, Type resultType,
29762976
resultType = PadOp::inferResultType(sourceType, staticLow, staticHigh);
29772977
}
29782978
assert(llvm::isa<RankedTensorType>(resultType));
2979+
result.addAttributes(attrs);
29792980
build(b, result, resultType, source, dynamicLow, dynamicHigh,
29802981
b.getDenseI64ArrayAttr(staticLow), b.getDenseI64ArrayAttr(staticHigh),
29812982
nofold ? b.getUnitAttr() : UnitAttr());
2982-
result.addAttributes(attrs);
29832983
}
29842984

29852985
void PadOp::build(OpBuilder &b, OperationState &result, Type resultType,
@@ -3423,11 +3423,11 @@ void ParallelInsertSliceOp::build(OpBuilder &b, OperationState &result,
34233423
dispatchIndexOpFoldResults(offsets, dynamicOffsets, staticOffsets);
34243424
dispatchIndexOpFoldResults(sizes, dynamicSizes, staticSizes);
34253425
dispatchIndexOpFoldResults(strides, dynamicStrides, staticStrides);
3426+
result.addAttributes(attrs);
34263427
build(b, result, {}, source, dest, dynamicOffsets, dynamicSizes,
34273428
dynamicStrides, b.getDenseI64ArrayAttr(staticOffsets),
34283429
b.getDenseI64ArrayAttr(staticSizes),
34293430
b.getDenseI64ArrayAttr(staticStrides));
3430-
result.addAttributes(attrs);
34313431
}
34323432

34333433
/// 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
@@ -2401,6 +2401,13 @@ def TableGenBuildOp5 : TableGenBuildInferReturnTypeBaseOp<
24012401
let regions = (region AnyRegion:$body);
24022402
}
24032403

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

0 commit comments

Comments
 (0)