Skip to content

Commit 33c33d0

Browse files
authored
[MLIR] Use IntValidAlignment for alignment attributes (#158137)
This PR refactors alignment validation in MLIR's MemRef and SPIRV dialects: - Use `IntValidAlignment` for consistent type safety across MemRef and SPIRV dialects - Eliminate duplicate validation logic in `MemRefOps.cpp` - Adjust error messages in `invalid.mlir` to match improved validation This is the first of two PRs addressing issue #155677.
1 parent 666e431 commit 33c33d0

File tree

5 files changed

+8
-18
lines changed

5 files changed

+8
-18
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ def MemRef_GlobalOp : MemRef_Op<"global", [Symbol]> {
11631163
MemRefTypeAttr:$type,
11641164
OptionalAttr<AnyAttr>:$initial_value,
11651165
UnitAttr:$constant,
1166-
OptionalAttr<I64Attr>:$alignment);
1166+
OptionalAttr<IntValidAlignment<I64Attr>>:$alignment);
11671167

11681168
let assemblyFormat = [{
11691169
($sym_visibility^)?
@@ -1231,8 +1231,7 @@ def LoadOp : MemRef_Op<"load",
12311231
[MemRead]>:$memref,
12321232
Variadic<Index>:$indices,
12331233
DefaultValuedOptionalAttr<BoolAttr, "false">:$nontemporal,
1234-
ConfinedAttr<OptionalAttr<I64Attr>,
1235-
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment);
1234+
OptionalAttr<IntValidAlignment<I64Attr>>:$alignment);
12361235

12371236
let builders = [
12381237
OpBuilder<(ins "Value":$memref,
@@ -1965,8 +1964,7 @@ def MemRef_StoreOp : MemRef_Op<"store",
19651964
[MemWrite]>:$memref,
19661965
Variadic<Index>:$indices,
19671966
DefaultValuedOptionalAttr<BoolAttr, "false">:$nontemporal,
1968-
ConfinedAttr<OptionalAttr<I64Attr>,
1969-
[AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment);
1967+
OptionalAttr<IntValidAlignment<I64Attr>>:$alignment);
19701968

19711969
let builders = [
19721970
OpBuilder<(ins "Value":$valueToStore,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def SPIRV_KHRCooperativeMatrixLoadOp : SPIRV_KhrVendorOp<"CooperativeMatrixLoad"
129129
SPIRV_KHR_CooperativeMatrixLayoutAttr:$matrix_layout,
130130
SPIRV_Integer:$stride,
131131
OptionalAttr<SPIRV_MemoryAccessAttr>:$memory_operand,
132-
OptionalAttr<I32Attr>:$alignment
132+
OptionalAttr<IntValidAlignment<I32Attr>>:$alignment
133133
);
134134

135135
let results = (outs
@@ -214,7 +214,7 @@ def SPIRV_KHRCooperativeMatrixStoreOp : SPIRV_KhrVendorOp<"CooperativeMatrixStor
214214
SPIRV_KHR_CooperativeMatrixLayoutAttr:$matrix_layout,
215215
SPIRV_Integer:$stride,
216216
OptionalAttr<SPIRV_MemoryAccessAttr>:$memory_operand,
217-
OptionalAttr<I32Attr>:$alignment
217+
OptionalAttr<IntValidAlignment<I32Attr>>:$alignment
218218
);
219219

220220
let results = (outs);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ def SPIRV_CopyMemoryOp : SPIRV_Op<"CopyMemory", []> {
121121
SPIRV_AnyPtr:$target,
122122
SPIRV_AnyPtr:$source,
123123
OptionalAttr<SPIRV_MemoryAccessAttr>:$memory_access,
124-
OptionalAttr<I32Attr>:$alignment,
124+
OptionalAttr<IntValidAlignment<I32Attr>>:$alignment,
125125
OptionalAttr<SPIRV_MemoryAccessAttr>:$source_memory_access,
126-
OptionalAttr<I32Attr>:$source_alignment
126+
OptionalAttr<IntValidAlignment<I32Attr>>:$source_alignment
127127
);
128128

129129
let results = (outs);

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,14 +1549,6 @@ LogicalResult GlobalOp::verify() {
15491549
}
15501550
}
15511551

1552-
if (std::optional<uint64_t> alignAttr = getAlignment()) {
1553-
uint64_t alignment = *alignAttr;
1554-
1555-
if (!llvm::isPowerOf2_64(alignment))
1556-
return emitError() << "alignment attribute value " << alignment
1557-
<< " is not a power of 2";
1558-
}
1559-
15601552
// TODO: verify visibility for declarations.
15611553
return success();
15621554
}

mlir/test/Dialect/MemRef/invalid.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func.func @mismatched_types() {
380380

381381
// -----
382382

383-
// expected-error @+1 {{alignment attribute value 63 is not a power of 2}}
383+
// expected-error @+1 {{'memref.global' op attribute 'alignment' failed to satisfy constraint: 64-bit signless integer attribute whose value is positive and whose value is a power of two > 0}}
384384
memref.global "private" @gv : memref<4xf32> = dense<1.0> { alignment = 63 }
385385

386386
// -----

0 commit comments

Comments
 (0)