Skip to content

Commit 36de962

Browse files
committed
add floating point trait for floating point operators for dealing with different floating point standards more programmably
1 parent 5a7f04a commit 36de962

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

include/circt/Dialect/Calyx/CalyxLoweringUtils.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,15 @@ class ComponentLoweringStateInterface {
424424
Block *body = component.getBodyBlock();
425425
builder.setInsertionPoint(body, body->begin());
426426
auto name = TLibraryOp::getOperationName().split(".").second;
427-
if (std::is_same<calyx::AddFNOp, TLibraryOp>::value)
428-
name = "std_addFN";
429-
else if (std::is_same<calyx::MulFNOp, TLibraryOp>::value)
430-
name = "std_mulFN";
427+
if constexpr (TLibraryOp::template hasTrait<FloatingPoint>()) {
428+
auto standard = TLibraryOp::getFloatingPointStandard();
429+
if (standard == FloatingPointStandard::IEEE754) {
430+
assert(name.consume_front("ieee754.") &&
431+
"IEEE754 type operation's name must begin with 'ieee754'");
432+
std::string modified = llvm::join_items("", "std_", name, "FN");
433+
name = modified;
434+
}
435+
}
431436
return builder.create<TLibraryOp>(loc, getUniqueName(name), resTypes);
432437
}
433438

include/circt/Dialect/Calyx/CalyxOps.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
namespace circt {
2727
namespace calyx {
2828

29+
class AddFNOp;
30+
class MulFNOp;
2931
// the goPort, donePort, resetPort and clkPort identify the attributes of the
3032
// go, done, reset and clk port of the circuit.
3133
static constexpr std::string_view goPort = "go";
@@ -67,6 +69,24 @@ class Combinational
6769
}
6870
};
6971

72+
enum class FloatingPointStandard {
73+
IEEE754,
74+
};
75+
76+
/// Signals that the following operation operates on floating point values.
77+
template <typename ConcreteType>
78+
class FloatingPoint
79+
: public mlir::OpTrait::TraitBase<ConcreteType, FloatingPoint> {
80+
public:
81+
static constexpr FloatingPointStandard getFloatingPointStandard() {
82+
if constexpr (std::is_same<calyx::AddFNOp, ConcreteType>::value ||
83+
std::is_same<calyx::MulFNOp, ConcreteType>::value) {
84+
return FloatingPointStandard::IEEE754;
85+
}
86+
return FloatingPointStandard::IEEE754;
87+
}
88+
};
89+
7090
/// The direction of a Component or Cell port. this is similar to the
7191
/// implementation found in the FIRRTL dialect.
7292
enum Direction { Input = 0, Output = 1 };

include/circt/Dialect/Calyx/CalyxPrimitives.td

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,15 @@ def AndLibOp : CombinationalArithBinaryLibraryOp<"and"> {}
332332
def OrLibOp : CombinationalArithBinaryLibraryOp<"or"> {}
333333
def XorLibOp : CombinationalArithBinaryLibraryOp<"xor"> {}
334334

335-
class ArithBinaryFloatingPointLibraryOp<string mnemonic> : ArithBinaryLibraryOp<mnemonic, "", [
336-
SameTypeConstraint<"left", "out">]> {}
335+
def FloatingPoint : NativeOpTrait<"FloatingPoint"> {
336+
let cppNamespace = "::circt::calyx";
337+
}
338+
339+
class ArithBinaryFloatingPointLibraryOp<string mnemonic> :
340+
ArithBinaryLibraryOp<mnemonic, "", [
341+
FloatingPoint,
342+
SameTypeConstraint<"left", "out">
343+
]> {}
337344

338345
def AddFNOp : ArithBinaryFloatingPointLibraryOp<"ieee754.add"> {
339346
let results = (outs I1:$clk, I1:$reset, I1:$go, I1:$control, I1:$subOp,

0 commit comments

Comments
 (0)