Skip to content

Commit 6ba42ae

Browse files
authored
Fix some AffineOps to properly declare their inherent affinemap Attribute (#66050)
This makes it more consistent with properties.
1 parent cb5bad2 commit 6ba42ae

File tree

6 files changed

+27
-57
lines changed

6 files changed

+27
-57
lines changed

mlir/include/mlir/Dialect/Affine/IR/AffineOps.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class AffineDmaStartOp
117117
/// Returns the affine map used to access the source memref.
118118
AffineMap getSrcMap() { return getSrcMapAttr().getValue(); }
119119
AffineMapAttr getSrcMapAttr() {
120-
return cast<AffineMapAttr>((*this)->getAttr(getSrcMapAttrStrName()));
120+
return cast<AffineMapAttr>(*(*this)->getInherentAttr(getSrcMapAttrStrName()));
121121
}
122122

123123
/// Returns the source memref affine map indices for this DMA operation.
@@ -156,7 +156,7 @@ class AffineDmaStartOp
156156
/// Returns the affine map used to access the destination memref.
157157
AffineMap getDstMap() { return getDstMapAttr().getValue(); }
158158
AffineMapAttr getDstMapAttr() {
159-
return cast<AffineMapAttr>((*this)->getAttr(getDstMapAttrStrName()));
159+
return cast<AffineMapAttr>(*(*this)->getInherentAttr(getDstMapAttrStrName()));
160160
}
161161

162162
/// Returns the destination memref indices for this DMA operation.
@@ -185,7 +185,7 @@ class AffineDmaStartOp
185185
/// Returns the affine map used to access the tag memref.
186186
AffineMap getTagMap() { return getTagMapAttr().getValue(); }
187187
AffineMapAttr getTagMapAttr() {
188-
return cast<AffineMapAttr>((*this)->getAttr(getTagMapAttrStrName()));
188+
return cast<AffineMapAttr>(*(*this)->getInherentAttr(getTagMapAttrStrName()));
189189
}
190190

191191
/// Returns the tag memref indices for this DMA operation.
@@ -307,7 +307,7 @@ class AffineDmaWaitOp
307307
/// Returns the affine map used to access the tag memref.
308308
AffineMap getTagMap() { return getTagMapAttr().getValue(); }
309309
AffineMapAttr getTagMapAttr() {
310-
return cast<AffineMapAttr>((*this)->getAttr(getTagMapAttrStrName()));
310+
return cast<AffineMapAttr>(*(*this)->getInherentAttr(getTagMapAttrStrName()));
311311
}
312312

313313
/// Returns the tag memref index for this DMA operation.

mlir/include/mlir/Dialect/Affine/IR/AffineOps.td

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -284,18 +284,18 @@ def AffineForOp : Affine_Op<"for",
284284

285285
/// Returns loop step.
286286
int64_t getStep() {
287-
return ::llvm::cast<IntegerAttr>((*this)->getAttr(getStepAttrStrName())).getInt();
287+
return ::llvm::cast<IntegerAttr>(*(*this)->getInherentAttr(getStepAttrStrName())).getInt();
288288
}
289289

290290
/// Returns affine map for the lower bound.
291291
AffineMap getLowerBoundMap() { return getLowerBoundMapAttr().getValue(); }
292292
AffineMapAttr getLowerBoundMapAttr() {
293-
return ::llvm::cast<AffineMapAttr>((*this)->getAttr(getLowerBoundAttrStrName()));
293+
return ::llvm::cast<AffineMapAttr>(*(*this)->getInherentAttr(getLowerBoundAttrStrName()));
294294
}
295295
/// Returns affine map for the upper bound. The upper bound is exclusive.
296296
AffineMap getUpperBoundMap() { return getUpperBoundMapAttr().getValue(); }
297297
AffineMapAttr getUpperBoundMapAttr() {
298-
return ::llvm::cast<AffineMapAttr>((*this)->getAttr(getUpperBoundAttrStrName()));
298+
return ::llvm::cast<AffineMapAttr>(*(*this)->getInherentAttr(getUpperBoundAttrStrName()));
299299
}
300300

301301
/// Set lower bound. The new bound must have the same number of operands as
@@ -497,7 +497,8 @@ class AffineLoadOpBase<string mnemonic, list<Trait> traits = []> :
497497
MemRefsNormalizable])> {
498498
let arguments = (ins Arg<AnyMemRef, "the reference to load from",
499499
[MemRead]>:$memref,
500-
Variadic<Index>:$indices);
500+
Variadic<Index>:$indices,
501+
AffineMapAttr:$map);
501502

502503
code extraClassDeclarationBase = [{
503504
/// Returns the operand index of the memref.
@@ -507,7 +508,7 @@ class AffineLoadOpBase<string mnemonic, list<Trait> traits = []> :
507508

508509
/// Returns the affine map used to index the memref for this operation.
509510
AffineMapAttr getAffineMapAttr() {
510-
return ::llvm::cast<AffineMapAttr>((*this)->getAttr(getMapAttrStrName()));
511+
return getProperties().map;
511512
}
512513

513514
static StringRef getMapAttrStrName() { return "map"; }
@@ -801,7 +802,8 @@ def AffinePrefetchOp : Affine_Op<"prefetch",
801802
BoolAttr:$isWrite,
802803
ConfinedAttr<I32Attr, [IntMinValue<0>,
803804
IntMaxValue<3>]>:$localityHint,
804-
BoolAttr:$isDataCache);
805+
BoolAttr:$isDataCache,
806+
AffineMapAttr:$map);
805807

806808
let builders = [
807809
OpBuilder<(ins "Value":$memref, "AffineMap":$map,
@@ -814,11 +816,12 @@ def AffinePrefetchOp : Affine_Op<"prefetch",
814816
auto isWriteAttr = $_builder.getBoolAttr(isWrite);
815817
auto isDataCacheAttr = $_builder.getBoolAttr(isDataCache);
816818
$_state.addOperands(memref);
817-
$_state.addAttribute(getMapAttrStrName(), AffineMapAttr::get(map));
818819
$_state.addOperands(mapOperands);
819-
$_state.addAttribute(getLocalityHintAttrStrName(), localityHintAttr);
820-
$_state.addAttribute(getIsWriteAttrStrName(), isWriteAttr);
821-
$_state.addAttribute(getIsDataCacheAttrStrName(), isDataCacheAttr);
820+
Properties &prop = $_state.getOrAddProperties<Properties>();
821+
prop.map = AffineMapAttr::get(map);
822+
prop.localityHint = localityHintAttr;
823+
prop.isWrite = isWriteAttr;
824+
prop.isDataCache = isDataCacheAttr;
822825
}]>];
823826

824827
let extraClassDeclaration = [{
@@ -829,10 +832,10 @@ def AffinePrefetchOp : Affine_Op<"prefetch",
829832
/// Returns the affine map used to index the memref for this operation.
830833
AffineMap getAffineMap() { return getAffineMapAttr().getValue(); }
831834
AffineMapAttr getAffineMapAttr() {
832-
return ::llvm::cast<AffineMapAttr>((*this)->getAttr(getMapAttrStrName()));
835+
return getProperties().map;
833836
}
834837

835-
/// Impelements the AffineMapAccessInterface.
838+
/// Implements the AffineMapAccessInterface.
836839
/// Returns the AffineMapAttr associated with 'memref'.
837840
NamedAttribute getAffineMapAttrForMemRef(Value mref) {
838841
assert(mref == getMemref() &&
@@ -874,7 +877,7 @@ class AffineStoreOpBase<string mnemonic, list<Trait> traits = []> :
874877

875878
/// Returns the affine map used to index the memref for this operation.
876879
AffineMapAttr getAffineMapAttr() {
877-
return ::llvm::cast<AffineMapAttr>((*this)->getAttr(getMapAttrStrName()));
880+
return getProperties().map;
878881
}
879882

880883
static StringRef getMapAttrStrName() { return "map"; }
@@ -912,7 +915,8 @@ def AffineStoreOp : AffineStoreOpBase<"store"> {
912915
let arguments = (ins AnyType:$value,
913916
Arg<AnyMemRef, "the reference to store to",
914917
[MemWrite]>:$memref,
915-
Variadic<Index>:$indices);
918+
Variadic<Index>:$indices,
919+
AffineMapAttr:$map);
916920

917921
let skipDefaultBuilders = 1;
918922
let builders = [
@@ -1065,7 +1069,8 @@ def AffineVectorStoreOp : AffineStoreOpBase<"vector_store"> {
10651069
let arguments = (ins AnyVector:$value,
10661070
Arg<AnyMemRef, "the reference to store to",
10671071
[MemWrite]>:$memref,
1068-
Variadic<Index>:$indices);
1072+
Variadic<Index>:$indices,
1073+
AffineMapAttr:$map);
10691074

10701075
let skipDefaultBuilders = 1;
10711076
let builders = [

mlir/include/mlir/IR/SymbolInterfaces.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def Symbol : OpInterface<"SymbolOpInterface"> {
163163
// If this is an optional symbol, bail out early if possible.
164164
auto concreteOp = cast<ConcreteOp>($_op);
165165
if (concreteOp.isOptionalSymbol()) {
166-
if(!concreteOp->getAttr(::mlir::SymbolTable::getSymbolAttrName()))
166+
if(!concreteOp->getInherentAttr(::mlir::SymbolTable::getSymbolAttrName()).value_or(Attribute{}))
167167
return success();
168168
}
169169
if (::mlir::failed(::mlir::detail::verifySymbol($_op)))

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3135,17 +3135,11 @@ static LogicalResult
31353135
verifyMemoryOpIndexing(Operation *op, AffineMapAttr mapAttr,
31363136
Operation::operand_range mapOperands,
31373137
MemRefType memrefType, unsigned numIndexOperands) {
3138-
if (mapAttr) {
31393138
AffineMap map = mapAttr.getValue();
31403139
if (map.getNumResults() != memrefType.getRank())
31413140
return op->emitOpError("affine map num results must equal memref rank");
31423141
if (map.getNumInputs() != numIndexOperands)
31433142
return op->emitOpError("expects as many subscripts as affine map inputs");
3144-
} else {
3145-
if (memrefType.getRank() != numIndexOperands)
3146-
return op->emitOpError(
3147-
"expects the number of subscripts to be equal to memref rank");
3148-
}
31493143

31503144
Region *scope = getAffineScope(op);
31513145
for (auto idx : mapOperands) {
@@ -3224,7 +3218,7 @@ void AffineStoreOp::build(OpBuilder &builder, OperationState &result,
32243218
result.addOperands(valueToStore);
32253219
result.addOperands(memref);
32263220
result.addOperands(mapOperands);
3227-
result.addAttribute(getMapAttrStrName(), AffineMapAttr::get(map));
3221+
result.getOrAddProperties<Properties>().map = AffineMapAttr::get(map);
32283222
}
32293223

32303224
// Use identity map.

mlir/test/Dialect/Affine/invalid.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func.func @affine_store_missing_l_square(%C: memref<4096x4096xf32>) {
145145
func.func @affine_store_wrong_value_type(%C: memref<f32>) {
146146
%c0 = arith.constant 0 : i32
147147
// expected-error@+1 {{value to store must have the same type as memref element type}}
148-
"affine.store"(%c0, %C) : (i32, memref<f32>) -> ()
148+
"affine.store"(%c0, %C) <{map = affine_map<(i) -> (i)>}> : (i32, memref<f32>) -> ()
149149
return
150150
}
151151

mlir/test/Dialect/Affine/load-store-invalid.mlir

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
// RUN: mlir-opt %s -split-input-file -verify-diagnostics
22

3-
func.func @load_too_many_subscripts(%arg0: memref<?x?xf32>, %arg1: index, %arg2: index, %arg3: index) {
4-
// expected-error@+1 {{expects the number of subscripts to be equal to memref rank}}
5-
"affine.load"(%arg0, %arg1, %arg2, %arg3) : (memref<?x?xf32>, index, index, index) -> f32
6-
}
7-
8-
// -----
9-
103
func.func @load_too_many_subscripts_map(%arg0: memref<?x?xf32>, %arg1: index, %arg2: index, %arg3: index) {
114
// expected-error@+1 {{op expects as many subscripts as affine map inputs}}
125
"affine.load"(%arg0, %arg1, %arg2, %arg3)
@@ -15,13 +8,6 @@ func.func @load_too_many_subscripts_map(%arg0: memref<?x?xf32>, %arg1: index, %a
158

169
// -----
1710

18-
func.func @load_too_few_subscripts(%arg0: memref<?x?xf32>, %arg1: index) {
19-
// expected-error@+1 {{expects the number of subscripts to be equal to memref rank}}
20-
"affine.load"(%arg0, %arg1) : (memref<?x?xf32>, index) -> f32
21-
}
22-
23-
// -----
24-
2511
func.func @load_too_few_subscripts_map(%arg0: memref<?x?xf32>, %arg1: index) {
2612
// expected-error@+1 {{op expects as many subscripts as affine map inputs}}
2713
"affine.load"(%arg0, %arg1)
@@ -30,14 +16,6 @@ func.func @load_too_few_subscripts_map(%arg0: memref<?x?xf32>, %arg1: index) {
3016

3117
// -----
3218

33-
func.func @store_too_many_subscripts(%arg0: memref<?x?xf32>, %arg1: index, %arg2: index,
34-
%arg3: index, %val: f32) {
35-
// expected-error@+1 {{expects the number of subscripts to be equal to memref rank}}
36-
"affine.store"(%val, %arg0, %arg1, %arg2, %arg3) : (f32, memref<?x?xf32>, index, index, index) -> ()
37-
}
38-
39-
// -----
40-
4119
func.func @store_too_many_subscripts_map(%arg0: memref<?x?xf32>, %arg1: index, %arg2: index,
4220
%arg3: index, %val: f32) {
4321
// expected-error@+1 {{op expects as many subscripts as affine map inputs}}
@@ -47,13 +25,6 @@ func.func @store_too_many_subscripts_map(%arg0: memref<?x?xf32>, %arg1: index, %
4725

4826
// -----
4927

50-
func.func @store_too_few_subscripts(%arg0: memref<?x?xf32>, %arg1: index, %val: f32) {
51-
// expected-error@+1 {{expects the number of subscripts to be equal to memref rank}}
52-
"affine.store"(%val, %arg0, %arg1) : (f32, memref<?x?xf32>, index) -> ()
53-
}
54-
55-
// -----
56-
5728
func.func @store_too_few_subscripts_map(%arg0: memref<?x?xf32>, %arg1: index, %val: f32) {
5829
// expected-error@+1 {{op expects as many subscripts as affine map inputs}}
5930
"affine.store"(%val, %arg0, %arg1)

0 commit comments

Comments
 (0)