Skip to content

Commit 402c8d3

Browse files
committed
Use a trait to avoid two-way dialect dependence
1 parent 0014ce5 commit 402c8d3

File tree

7 files changed

+56
-3
lines changed

7 files changed

+56
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define MLIR_DIALECT_AFFINE_IR_AFFINEOPS_H
1616

1717
#include "mlir/Dialect/Affine/IR/AffineMemoryOpInterfaces.h"
18+
#include "mlir/Dialect/Affine/IR/AffineTraits.h"
1819
#include "mlir/Dialect/Arith/IR/Arith.h"
1920
#include "mlir/Dialect/Utils/StaticValueUtils.h"
2021
#include "mlir/IR/AffineMap.h"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef AFFINE_OPS
1414
#define AFFINE_OPS
1515

16+
include "mlir/Dialect/Affine/IR/AffineTraits.td"
1617
include "mlir/Dialect/Arith/IR/ArithBase.td"
1718
include "mlir/Dialect/Affine/IR/AffineMemoryOpInterfaces.td"
1819
include "mlir/Interfaces/ControlFlowInterfaces.td"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===- AffineTraits.h - MLIR Affine Traits --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines traits brought in by the Affine dialect.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
#ifndef AFFINE_TRAITS_H
13+
#define AFFINE_TRAITS_H
14+
15+
#include "mlir/IR/OpDefinition.h"
16+
17+
namespace mlir::OpTrait {
18+
19+
template <typename ConcreteType>
20+
class AffineDim : public TraitBase<ConcreteType, AffineDim> {
21+
public:
22+
static LogicalResult verifyTrait(Operation *op) { return success(); }
23+
};
24+
25+
} // namespace mlir::OpTrait
26+
27+
#endif // AFFINE_TRAITS_H
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===- AffineTraits.td - Affine dialect traits -------------*- tablegen -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Defines traits brought in by the MLIR Affine dialect.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
#ifndef AFFINE_TRAITS
13+
#define AFFINE_TRAITS
14+
15+
include "mlir/IR/OpBase.td"
16+
17+
// Trait to declare that an op result is an affine dimension identifier.
18+
// Prevents the result from being seen as a symbol into AffineMaps
19+
// and IntegerSets.
20+
def AffineDim : NativeOpTrait<"AffineDim">;
21+
22+
#endif // AFFINE_TRAITS

mlir/include/mlir/Dialect/Linalg/IR/Linalg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define MLIR_DIALECT_LINALG_IR_LINALG_H
1111

1212
#include "mlir/Bytecode/BytecodeOpInterface.h"
13+
#include "mlir/Dialect/Affine/IR/AffineTraits.h"
1314
#include "mlir/Dialect/Utils/ReshapeOpsUtils.h"
1415
#include "mlir/Dialect/Utils/StructuredOpsUtils.h"
1516
#include "mlir/IR/AffineExpr.h"

mlir/include/mlir/Dialect/Linalg/IR/LinalgOps.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LINALG_OPS
1414
#define LINALG_OPS
1515

16+
include "mlir/Dialect/Affine/IR/AffineTraits.td"
1617
include "mlir/Dialect/Linalg/IR/LinalgBase.td"
1718
include "mlir/Dialect/Linalg/IR/LinalgInterfaces.td"
1819
include "mlir/Interfaces/ControlFlowInterfaces.td"
@@ -46,7 +47,7 @@ def Linalg_YieldOp : Linalg_Op<"yield", [Pure, ReturnLike, Terminator]>,
4647
let hasVerifier = 1;
4748
}
4849

49-
def Linalg_IndexOp : Linalg_Op<"index", [Pure]>,
50+
def Linalg_IndexOp : Linalg_Op<"index", [Pure, AffineDim]>,
5051
Arguments<(ins ConfinedAttr<I64Attr, [IntMinValue<0>]>:$dim)>,
5152
Results<(outs Index:$result)> {
5253
let summary = "linalg index operation";

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ bool mlir::affine::isValidDim(Value value, Region *region) {
314314
}
315315

316316
// Remove me: linalg.index ops are valid affine dim identifiers
317-
if (isa<linalg::IndexOp>(op))
317+
if (op->hasTrait<OpTrait::AffineDim>())
318318
return true;
319319

320320
// Affine apply operation is ok if all of its operands are ok.
@@ -445,7 +445,7 @@ bool mlir::affine::isValidSymbol(Value value, Region *region) {
445445
}
446446

447447
// Remove me: linalg.index ops are not valid affine symbols
448-
if (isa<linalg::IndexOp>(defOp))
448+
if (defOp->hasTrait<OpTrait::AffineDim>())
449449
return false;
450450

451451
// Constant operation is ok.

0 commit comments

Comments
 (0)