[Mlir-commits] [mlir] [mlir][vector] Improve vector dialect documentation and constraint predicates (PR #155259)
Erick Ochoa Lopez
llvmlistbot at llvm.org
Mon Aug 25 08:29:55 PDT 2025
https://github.com/amd-eochoalo created https://github.com/llvm/llvm-project/pull/155259
This PR changes the documentation of the vector dialect to better reflect reality. When vector operations with alignment requirements are violated, undefined behavior will occur.
This PR also adds the predicate `IntValidAlignment` and adds it as constraint predicates for alignment attribtues.
>From fad2bb3044f6e35ae3b2207edaed145f1567cc13 Mon Sep 17 00:00:00 2001
From: Erick Ochoa <erick.ochoalopez at amd.com>
Date: Mon, 25 Aug 2025 07:55:28 -0700
Subject: [PATCH 1/2] [mlir][vector] Improve documentation on violation of
alignment requirements.
---
.../mlir/Dialect/Vector/IR/VectorOps.td | 32 +++++++++----------
1 file changed, 16 insertions(+), 16 deletions(-)
diff --git a/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td b/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
index bcc423a634148..71dbcd8d564bb 100644
--- a/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
+++ b/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
@@ -1712,8 +1712,8 @@ def Vector_LoadOp : Vector_Op<"load", [
An optional `alignment` attribute allows to specify the byte alignment of the
load operation. It must be a positive power of 2. The operation must access
- memory at an address aligned to this boundary. Violations may lead to
- architecture-specific faults or performance penalties.
+ memory at an address aligned to this boundary. Violating this requirement
+ triggers immediate undefined behavior.
}];
let arguments = (ins Arg<AnyMemRef, "the reference to load from",
@@ -1827,8 +1827,8 @@ def Vector_StoreOp : Vector_Op<"store", [
An optional `alignment` attribute allows to specify the byte alignment of the
store operation. It must be a positive power of 2. The operation must access
- memory at an address aligned to this boundary. Violations may lead to
- architecture-specific faults or performance penalties.
+ memory at an address aligned to this boundary. Violating this requirement
+ triggers immediate undefined behavior.
}];
let arguments = (ins
@@ -1915,8 +1915,8 @@ def Vector_MaskedLoadOp :
An optional `alignment` attribute allows to specify the byte alignment of the
load operation. It must be a positive power of 2. The operation must access
- memory at an address aligned to this boundary. Violations may lead to
- architecture-specific faults or performance penalties.
+ memory at an address aligned to this boundary. Violating this requirement
+ triggers immediate undefined behavior.
}];
let extraClassDeclaration = [{
MemRefType getMemRefType() {
@@ -2007,8 +2007,8 @@ def Vector_MaskedStoreOp :
An optional `alignment` attribute allows to specify the byte alignment of the
store operation. It must be a positive power of 2. The operation must access
- memory at an address aligned to this boundary. Violations may lead to
- architecture-specific faults or performance penalties.
+ memory at an address aligned to this boundary. Violating this requirement
+ triggers immediate undefined behavior.
}];
let extraClassDeclaration = [{
MemRefType getMemRefType() {
@@ -2100,8 +2100,8 @@ def Vector_GatherOp :
An optional `alignment` attribute allows to specify the byte alignment of the
gather operation. It must be a positive power of 2. The operation must access
- memory at an address aligned to this boundary. Violations may lead to
- architecture-specific faults or performance penalties.
+ memory at an address aligned to this boundary. Violating this requirement
+ triggers immediate undefined behavior.
Examples:
@@ -2191,8 +2191,8 @@ def Vector_ScatterOp :
An optional `alignment` attribute allows to specify the byte alignment of the
scatter operation. It must be a positive power of 2. The operation must access
- memory at an address aligned to this boundary. Violations may lead to
- architecture-specific faults or performance penalties.
+ memory at an address aligned to this boundary. Violating this requirement
+ triggers immediate undefined behavior.
Examples:
@@ -2274,8 +2274,8 @@ def Vector_ExpandLoadOp :
An optional `alignment` attribute allows to specify the byte alignment of the
load operation. It must be a positive power of 2. The operation must access
- memory at an address aligned to this boundary. Violations may lead to
- architecture-specific faults or performance penalties.
+ memory at an address aligned to this boundary. Violating this requirement
+ triggers immediate undefined behavior.
Note, at the moment this Op is only available for fixed-width vectors.
@@ -2362,8 +2362,8 @@ def Vector_CompressStoreOp :
An optional `alignment` attribute allows to specify the byte alignment of the
store operation. It must be a positive power of 2. The operation must access
- memory at an address aligned to this boundary. Violations may lead to
- architecture-specific faults or performance penalties.
+ memory at an address aligned to this boundary. Violating this requirement
+ triggers immediate undefined behavior.
Note, at the moment this Op is only available for fixed-width vectors.
>From c3d9d6c98453a8a88cfb7f36d4bc4f72dfd524f7 Mon Sep 17 00:00:00 2001
From: Erick Ochoa <erick.ochoalopez at amd.com>
Date: Mon, 25 Aug 2025 08:26:08 -0700
Subject: [PATCH 2/2] Add IntValidAlignment predicate
---
.../mlir/Dialect/Vector/IR/VectorOps.td | 24 +++++++------------
mlir/include/mlir/IR/CommonAttrConstraints.td | 4 ++++
2 files changed, 12 insertions(+), 16 deletions(-)
diff --git a/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td b/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
index 71dbcd8d564bb..f650a51840cf5 100644
--- a/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
+++ b/mlir/include/mlir/Dialect/Vector/IR/VectorOps.td
@@ -1720,8 +1720,7 @@ def Vector_LoadOp : Vector_Op<"load", [
[MemRead]>:$base,
Variadic<Index>:$indices,
DefaultValuedOptionalAttr<BoolAttr, "false">:$nontemporal,
- ConfinedAttr<OptionalAttr<I64Attr>,
- [AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment);
+ OptionalAttr<IntValidAlignment<I64Attr>>: $alignment);
let builders = [
OpBuilder<(ins "VectorType":$resultType,
@@ -1837,8 +1836,7 @@ def Vector_StoreOp : Vector_Op<"store", [
[MemWrite]>:$base,
Variadic<Index>:$indices,
DefaultValuedOptionalAttr<BoolAttr, "false">:$nontemporal,
- ConfinedAttr<OptionalAttr<I64Attr>,
- [AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment);
+ OptionalAttr<IntValidAlignment<I64Attr>>: $alignment);
let builders = [
OpBuilder<(ins "Value":$valueToStore,
@@ -1875,8 +1873,7 @@ def Vector_MaskedLoadOp :
Variadic<Index>:$indices,
VectorOfNonZeroRankOf<[I1]>:$mask,
AnyVectorOfNonZeroRank:$pass_thru,
- ConfinedAttr<OptionalAttr<I64Attr>,
- [AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)>,
+ OptionalAttr<IntValidAlignment<I64Attr>>: $alignment)>,
Results<(outs AnyVectorOfNonZeroRank:$result)> {
let summary = "loads elements from memory into a vector as defined by a mask vector";
@@ -1968,8 +1965,7 @@ def Vector_MaskedStoreOp :
Variadic<Index>:$indices,
VectorOfNonZeroRankOf<[I1]>:$mask,
AnyVectorOfNonZeroRank:$valueToStore,
- ConfinedAttr<OptionalAttr<I64Attr>,
- [AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)> {
+ OptionalAttr<IntValidAlignment<I64Attr>>: $alignment)> {
let summary = "stores elements from a vector into memory as defined by a mask vector";
@@ -2051,8 +2047,7 @@ def Vector_GatherOp :
VectorOfNonZeroRankOf<[AnyInteger, Index]>:$index_vec,
VectorOfNonZeroRankOf<[I1]>:$mask,
AnyVectorOfNonZeroRank:$pass_thru,
- ConfinedAttr<OptionalAttr<I64Attr>,
- [AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)>,
+ OptionalAttr<IntValidAlignment<I64Attr>>: $alignment)>,
Results<(outs AnyVectorOfNonZeroRank:$result)> {
let summary = [{
@@ -2154,8 +2149,7 @@ def Vector_ScatterOp :
VectorOfNonZeroRankOf<[AnyInteger, Index]>:$index_vec,
VectorOfNonZeroRankOf<[I1]>:$mask,
AnyVectorOfNonZeroRank:$valueToStore,
- ConfinedAttr<OptionalAttr<I64Attr>,
- [AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)> {
+ OptionalAttr<IntValidAlignment<I64Attr>>: $alignment)> {
let summary = [{
scatters elements from a vector into memory as defined by an index vector
@@ -2239,8 +2233,7 @@ def Vector_ExpandLoadOp :
Variadic<Index>:$indices,
FixedVectorOfNonZeroRankOf<[I1]>:$mask,
AnyVectorOfNonZeroRank:$pass_thru,
- ConfinedAttr<OptionalAttr<I64Attr>,
- [AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)>,
+ OptionalAttr<IntValidAlignment<I64Attr>>: $alignment)>,
Results<(outs AnyVectorOfNonZeroRank:$result)> {
let summary = "reads elements from memory and spreads them into a vector as defined by a mask";
@@ -2328,8 +2321,7 @@ def Vector_CompressStoreOp :
Variadic<Index>:$indices,
FixedVectorOfNonZeroRankOf<[I1]>:$mask,
AnyVectorOfNonZeroRank:$valueToStore,
- ConfinedAttr<OptionalAttr<I64Attr>,
- [AllAttrOf<[IntPositive, IntPowerOf2]>]>:$alignment)> {
+ OptionalAttr<IntValidAlignment<I64Attr>>: $alignment)> {
let summary = "writes elements selectively from a vector as defined by a mask";
diff --git a/mlir/include/mlir/IR/CommonAttrConstraints.td b/mlir/include/mlir/IR/CommonAttrConstraints.td
index 18da85a580710..e1869c1821b11 100644
--- a/mlir/include/mlir/IR/CommonAttrConstraints.td
+++ b/mlir/include/mlir/IR/CommonAttrConstraints.td
@@ -800,6 +800,10 @@ def IntPowerOf2 : AttrConstraint<
CPred<"::llvm::cast<::mlir::IntegerAttr>($_self).getValue().isPowerOf2()">,
"whose value is a power of two > 0">;
+def IntPositivePowerOf2 : AllAttrOf<[IntPositive, IntPowerOf2]>;
+
+class IntValidAlignment<Attr attr>: ConfinedAttr<attr, [IntPositivePowerOf2]>;
+
class ArrayMaxCount<int n> : AttrConstraint<
CPred<"::llvm::cast<::mlir::ArrayAttr>($_self).size() <= " # n>,
"with at most " # n # " elements">;
More information about the Mlir-commits
mailing list