Skip to content

Commit 120dd53

Browse files
authored
[RTG] Add ImplicitConstraintOpInterface (#9205)
1 parent f23f386 commit 120dd53

File tree

7 files changed

+118
-0
lines changed

7 files changed

+118
-0
lines changed

include/circt/Dialect/RTG/IR/RTGInterfaces.td

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,28 @@ def ValidationTypeInterface : TypeInterface<"ValidationTypeInterface"> {
8181
];
8282
}
8383

84+
def ImplicitConstraintOpInterface : OpInterface<"ImplicitConstraintOpInterface"> {
85+
let description = [{
86+
This interface should be implemented by operations that implicitly enforce
87+
a constraint. This is used to automatically materialize constraints that
88+
are not explicitly specified by the user.
89+
}];
90+
let cppNamespace = "::circt::rtg";
91+
92+
let methods = [
93+
InterfaceMethod<[{
94+
Returns whether the constraint enforced by this operation has already
95+
been materialized.
96+
}], "bool", "isConstraintMaterialized">,
97+
InterfaceMethod<[{
98+
Materialize the constraint enforced by this operation. The operation
99+
itself may be modified in place (return itself) or a new operation may
100+
be returned to be replaced with this operation. Returning `nullptr` means
101+
the operation should be erased without being replaced (only valid for
102+
operations without results).
103+
}], "mlir::Operation *", "materializeConstraint", (ins
104+
"mlir::OpBuilder &":$builder)>,
105+
];
106+
}
107+
84108
#endif // CIRCT_DIALECT_RTG_IR_RTGINTERFACES_TD

include/circt/Dialect/RTG/Transforms/RTGPasses.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,8 @@ def SimpleTestInlinerPass : Pass<"rtg-simple-test-inliner", "mlir::ModuleOp"> {
228228
}];
229229
}
230230

231+
def MaterializeConstraintsPass : Pass<"rtg-materialize-constraints"> {
232+
let summary = "materialize implicit constraints";
233+
}
234+
231235
#endif // CIRCT_DIALECT_RTG_TRANSFORMS_RTGPASSES_TD

include/circt/Dialect/RTGTest/IR/RTGTestOps.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ def GetHartIdOp : RTGTestOp<"get_hartid", [Pure]> {
3939
let hasFolder = 1;
4040
}
4141

42+
def ImplicitConstraintTestOp : RTGTestOp<"implicit_constraint_op", [
43+
DeclareOpInterfaceMethods<ImplicitConstraintOpInterface>,
44+
]> {
45+
let arguments = (ins UnitAttr:$implicitConstraint);
46+
let assemblyFormat = "(`implicit_constraint` $implicitConstraint^)? attr-dict";
47+
}
48+
4249
//===- Instruction Formats -------------------------------------------------===//
4350

4451
class InstFormatIOpBase<string mnemonic, int opcode7, int funct3>

lib/Dialect/RTG/Transforms/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_circt_dialect_library(CIRCTRTGTransforms
77
LinearScanRegisterAllocationPass.cpp
88
LowerUniqueLabelsPass.cpp
99
LowerValidateToLabelsPass.cpp
10+
MaterializeConstraintsPass.cpp
1011
MemoryAllocationPass.cpp
1112
PrintTestNamesPass.cpp
1213
RTGPassPipelines.cpp
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===----------------------------------------------------------------------===//
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+
#include "circt/Dialect/RTG/IR/RTGOpInterfaces.h"
10+
#include "circt/Dialect/RTG/Transforms/RTGPasses.h"
11+
#include "mlir/IR/PatternMatch.h"
12+
13+
namespace circt {
14+
namespace rtg {
15+
#define GEN_PASS_DEF_MATERIALIZECONSTRAINTSPASS
16+
#include "circt/Dialect/RTG/Transforms/RTGPasses.h.inc"
17+
} // namespace rtg
18+
} // namespace circt
19+
20+
using namespace mlir;
21+
using namespace circt;
22+
using namespace circt::rtg;
23+
24+
//===----------------------------------------------------------------------===//
25+
// Materialize Constraints Pass
26+
//===----------------------------------------------------------------------===//
27+
28+
namespace {
29+
struct MaterializeConstraintsPass
30+
: public rtg::impl::MaterializeConstraintsPassBase<
31+
MaterializeConstraintsPass> {
32+
using Base::Base;
33+
void runOnOperation() override;
34+
};
35+
} // namespace
36+
37+
void MaterializeConstraintsPass::runOnOperation() {
38+
getOperation()->walk([&](ImplicitConstraintOpInterface op) {
39+
if (op.isConstraintMaterialized())
40+
return;
41+
42+
OpBuilder builder(op);
43+
builder.setInsertionPointAfter(op);
44+
auto *newOp = op.materializeConstraint(builder);
45+
if (newOp == op)
46+
return;
47+
if (newOp && op->getNumResults() > 0)
48+
op->replaceAllUsesWith(newOp);
49+
assert(newOp ||
50+
op->getNumResults() == 0 &&
51+
"cannot erase operation without result value replacements");
52+
op->erase();
53+
});
54+
}

lib/Dialect/RTGTest/IR/RTGTestOps.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@ mlir::OpFoldResult GetHartIdOp::fold(FoldAdaptor adaptor) {
3535
return {};
3636
}
3737

38+
//===----------------------------------------------------------------------===//
39+
// ImplicitConstraintTestOp
40+
//===----------------------------------------------------------------------===//
41+
42+
bool ImplicitConstraintTestOp::isConstraintMaterialized() {
43+
return !getImplicitConstraint();
44+
}
45+
46+
Operation *ImplicitConstraintTestOp::materializeConstraint(OpBuilder &builder) {
47+
auto val =
48+
rtg::ConstantOp::create(builder, getLoc(), builder.getBoolAttr(true));
49+
rtg::ConstraintOp::create(builder, getLoc(), val);
50+
setImplicitConstraint(false);
51+
return getOperation();
52+
}
53+
3854
//===----------------------------------------------------------------------===//
3955
// TableGen generated logic.
4056
//===----------------------------------------------------------------------===//
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: circt-opt %s --rtg-materialize-constraints | FileCheck %s
2+
3+
// CHECK-LABEL: @test
4+
rtg.test @test() {
5+
// CHECK-NEXT: rtgtest.implicit_constraint_op{{$}}
6+
// CHECK-NEXT: [[V0:%.+]] = rtg.constant true
7+
// CHECK-NEXT: rtg.constraint [[V0]]
8+
rtgtest.implicit_constraint_op implicit_constraint
9+
// CHECK-NEXT: rtgtest.implicit_constraint_op{{$}}
10+
rtgtest.implicit_constraint_op
11+
// CHECK-NEXT: }
12+
}

0 commit comments

Comments
 (0)