Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RTGTest] Add integer register type API #8141

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions include/circt-c/Dialect/RTG.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ MLIR_CAPI_EXPORTED MlirType rtgDictTypeGet(MlirContext ctxt,
MlirAttribute const *entryNames,
MlirType const *entryTypes);

//===----------------------------------------------------------------------===//
// Attribute API.
//===----------------------------------------------------------------------===//

enum RTGLabelVisibility {
RTG_LABEL_VISIBILITY_LOCAL,
RTG_LABEL_VISIBILITY_GLOBAL,
RTG_LABEL_VISIBILITY_EXTERNAL
};
typedef enum RTGLabelVisibility RTGLabelVisibility;

/// If the attribute is an RTG label visibility.
MLIR_CAPI_EXPORTED bool rtgAttrIsALabelVisibilityAttr(MlirAttribute attr);

/// Get the RTG label visibility from the attribute.
MLIR_CAPI_EXPORTED RTGLabelVisibility
rtgLabelVisibilityAttrGetValue(MlirAttribute attr);

/// Creates an RTG label visibility attribute in the context.
MLIR_CAPI_EXPORTED MlirAttribute
rtgLabelVisibilityAttrGet(MlirContext ctxt, RTGLabelVisibility visibility);

#ifdef __cplusplus
}
#endif
Expand Down
6 changes: 6 additions & 0 deletions include/circt-c/Dialect/RTGTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ MLIR_CAPI_EXPORTED bool rtgtestTypeIsACPU(MlirType type);
/// Creates an RTGTest CPU type in the context.
MLIR_CAPI_EXPORTED MlirType rtgtestCPUTypeGet(MlirContext ctxt);

/// If the type is an RTGTest IntegerRegisterType.
MLIR_CAPI_EXPORTED bool rtgtestTypeIsAIntegerRegister(MlirType type);

/// Creates an RTGTest IntegerRegisterType in the context.
MLIR_CAPI_EXPORTED MlirType rtgtestIntegerRegisterTypeGet(MlirContext ctxt);

// Immediates.
//===----------------------------------------------------------------------===//

Expand Down
8 changes: 7 additions & 1 deletion include/circt-c/RtgTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ circtRtgToolOptionsSetVerbosePassExecution(CirctRtgToolOptions options,

MLIR_CAPI_EXPORTED void circtRtgToolOptionsSetUnsupportedInstructions(
CirctRtgToolOptions options, unsigned numInstr,
const char **unsupportedInstructions);
const void **unsupportedInstructions);

MLIR_CAPI_EXPORTED void circtRtgToolOptionsAddUnsupportedInstruction(
CirctRtgToolOptions options, const char *unsupportedInstruction);
Expand All @@ -65,6 +65,12 @@ MLIR_CAPI_EXPORTED void
circtRtgToolOptionsSetUnsupportedInstructionsFile(CirctRtgToolOptions options,
const char *filename);

MLIR_CAPI_EXPORTED void
circtRtgToolOptionsSetSplitOutput(CirctRtgToolOptions options, bool enable);

MLIR_CAPI_EXPORTED void
circtRtgToolOptionsSetOutputPath(CirctRtgToolOptions options, const char *path);

//===----------------------------------------------------------------------===//
// Pipeline Population API.
//===----------------------------------------------------------------------===//
Expand Down
14 changes: 14 additions & 0 deletions include/circt/Tools/rtgtool/RtgToolOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,27 @@ class RtgToolOptions {
return unsupportedInstructionsFile;
}

RtgToolOptions &setSplitOutput(bool enable) {
splitOutput = enable;
return *this;
}
bool getSplitOutput() const { return splitOutput; }

RtgToolOptions &setOutputPath(StringRef path) {
outputPath = path;
return *this;
}
std::string getOutputPath() const { return outputPath; }

private:
OutputFormat outputFormat = OutputFormat::ElaboratedMLIR;
unsigned seed;
bool verifyPasses = true;
bool verbosePassExecution = false;
SmallVector<std::string> unsupportedInstructions;
std::string unsupportedInstructionsFile;
bool splitOutput = false;
std::string outputPath;
};

/// Populates the passes necessary to lower IR with RTG randomization operations
Expand Down
23 changes: 21 additions & 2 deletions integration_test/Bindings/Python/dialects/rtg.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@
labelTy = rtg.LabelType.get()
setTy = rtg.SetType.get(indexTy)
bagTy = rtg.BagType.get(indexTy)
ireg = rtgtest.IntegerRegisterType.get()
seq = rtg.SequenceOp('seq')
Block.create_at_start(seq.bodyRegion, [sequenceTy, labelTy, setTy, bagTy])
Block.create_at_start(seq.bodyRegion,
[sequenceTy, labelTy, setTy, bagTy, ireg])

# CHECK: rtg.sequence @seq
# CHECK: (%{{.*}}: !rtg.sequence, %{{.*}}: !rtg.label, %{{.*}}: !rtg.set<index>, %{{.*}}: !rtg.bag<index>):
# CHECK: (%{{.*}}: !rtg.sequence, %{{.*}}: !rtg.label, %{{.*}}: !rtg.set<index>, %{{.*}}: !rtg.bag<index>, %{{.*}}: !rtgtest.ireg):
print(m)

with Context() as ctx, Location.unknown():
Expand Down Expand Up @@ -180,3 +182,20 @@
rtgtest.ImmediateOp(rtgtest.Imm32Attr.get(3))

print(m)

with Context() as ctx, Location.unknown():
circt.register_dialects(ctx)
m = Module.create()
with InsertionPoint(m.body):
seq = rtg.SequenceOp('seq')
block = Block.create_at_start(seq.bodyRegion, [])
with InsertionPoint(block):
l = rtg.label_decl("label", [])
visibility = rtg.LabelVisibilityAttr.get(rtg.GLOBAL)
rtg.label(visibility, l)
assert visibility.value == rtg.GLOBAL

# CHECK: rtg.sequence @seq
# CHECK: rtg.label_decl "label"
# CHECK: rtg.label global {{%.+}}
print(m)
41 changes: 41 additions & 0 deletions integration_test/Bindings/Python/rtg_pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# REQUIRES: bindings_python
# RUN: %PYTHON% %s %T && FileCheck %s --input-file=%T/test0.s --check-prefix=TEST0 && FileCheck %s --input-file=%T/test1.s --check-prefix=TEST1

import sys
import circt

from circt.dialects import rtg, rtgtest
from circt.ir import Context, Location, Module, InsertionPoint, Block, TypeAttr
from circt.passmanager import PassManager
from circt import rtgtool_support as rtgtool

# Tests the split_file option and that the strings of unsupported instructions
# are passed properly to the emission pass.
with Context() as ctx, Location.unknown():
circt.register_dialects(ctx)
m = Module.create()
with InsertionPoint(m.body):
test = rtg.TestOp('test0', TypeAttr.get(rtg.DictType.get()))
block = Block.create_at_start(test.bodyRegion, [])
with InsertionPoint(block):
rtgtest.rv32i_ebreak()

test = rtg.TestOp('test1', TypeAttr.get(rtg.DictType.get()))
block = Block.create_at_start(test.bodyRegion, [])
with InsertionPoint(block):
rtgtest.rv32i_ecall()

pm = PassManager()
options = rtgtool.Options(seed=0,
output_format=rtgtool.OutputFormat.ASM,
split_output=True,
unsupported_instructions=['rtgtest.rv32i.ebreak'],
output_path=sys.argv[1])
rtgtool.populate_randomizer_pipeline(pm, options)
pm.run(m.operation)

# TEST0: ebreak
# TEST0: .word 0x

# TEST1: ecall
print(m)
18 changes: 18 additions & 0 deletions lib/Bindings/Python/RTGModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,22 @@ void circt::python::populateDialectRTGSubmodule(nb::module_ &m) {
nb::arg("self"), nb::arg("ctxt") = nullptr,
nb::arg("entries") =
std::vector<std::pair<MlirAttribute, MlirType>>());

nb::enum_<RTGLabelVisibility>(m, "LabelVisibility")
.value("LOCAL", RTG_LABEL_VISIBILITY_LOCAL)
.value("GLOBAL", RTG_LABEL_VISIBILITY_GLOBAL)
.value("EXTERNAL", RTG_LABEL_VISIBILITY_EXTERNAL)
.export_values();

mlir_attribute_subclass(m, "LabelVisibilityAttr",
rtgAttrIsALabelVisibilityAttr)
.def_classmethod(
"get",
[](nb::object cls, RTGLabelVisibility visibility, MlirContext ctxt) {
return cls(rtgLabelVisibilityAttrGet(ctxt, visibility));
},
nb::arg("self"), nb::arg("visibility"), nb::arg("ctxt") = nullptr)
.def_property_readonly("value", [](MlirAttribute self) {
return rtgLabelVisibilityAttrGetValue(self);
});
}
8 changes: 8 additions & 0 deletions lib/Bindings/Python/RTGTestModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ void circt::python::populateDialectRTGTestSubmodule(nb::module_ &m) {
},
nb::arg("self"), nb::arg("ctxt") = nullptr);

mlir_type_subclass(m, "IntegerRegisterType", rtgtestTypeIsAIntegerRegister)
.def_classmethod(
"get",
[](nb::object cls, MlirContext ctxt) {
return cls(rtgtestIntegerRegisterTypeGet(ctxt));
},
nb::arg("self"), nb::arg("ctxt") = nullptr);

mlir_type_subclass(m, "Imm12Type", rtgtestTypeIsAImm12)
.def_classmethod(
"get",
Expand Down
33 changes: 27 additions & 6 deletions lib/Bindings/Python/RTGToolModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ class PyRtgToolOptions {
public:
PyRtgToolOptions(unsigned seed, CirctRtgToolOutputFormat outputFormat,
bool verifyPasses, bool verbosePassExecution,
const std::vector<const char *> &unsupportedInstructions,
const std::string &unsupportedInstructionsFile)
const std::vector<std::string> &unsupportedInstructions,
const std::string &unsupportedInstructionsFile,
bool splitOutput, const std::string &outputPath)
: options(circtRtgToolOptionsCreateDefault(seed)) {
setOutputFormat(outputFormat);
setVerifyPasses(verifyPasses);
setVerbosePassExecution(verbosePassExecution);
setUnsupportedInstructions(unsupportedInstructions);
setUnsupportedInstructionsFile(unsupportedInstructionsFile);
setSplitOutput(splitOutput);
setOutputPath(outputPath);
}
~PyRtgToolOptions() { circtRtgToolOptionsDestroy(options); }

Expand All @@ -52,10 +55,11 @@ class PyRtgToolOptions {
}

void
setUnsupportedInstructions(const std::vector<const char *> &instructions) {
setUnsupportedInstructions(const std::vector<std::string> &instructions) {
circtRtgToolOptionsSetUnsupportedInstructions(
options, instructions.size(),
const_cast<const char **>(instructions.data()));
reinterpret_cast<const void **>(
const_cast<std::string *>(instructions.data())));
}

void addUnsupportedInstruction(const std::string &instruction) {
Expand All @@ -67,6 +71,14 @@ class PyRtgToolOptions {
filename.c_str());
}

void setSplitOutput(bool enable) {
circtRtgToolOptionsSetSplitOutput(options, enable);
}

void setOutputPath(const std::string &path) {
circtRtgToolOptionsSetOutputPath(options, path.c_str());
}

private:
CirctRtgToolOptions options;
};
Expand All @@ -83,13 +95,15 @@ void circt::python::populateDialectRTGToolSubmodule(nb::module_ &m) {

nb::class_<PyRtgToolOptions>(m, "Options")
.def(nb::init<unsigned, CirctRtgToolOutputFormat, bool, bool,
const std::vector<const char *> &, const std::string &>(),
const std::vector<std::string> &, const std::string &, bool,
const std::string &>(),
nb::arg("seed"),
nb::arg("output_format") = CIRCT_RTGTOOL_OUTPUT_FORMAT_ASM,
nb::arg("verify_passes") = true,
nb::arg("verbose_pass_execution") = false,
nb::arg("unsupported_instructions") = std::vector<const char *>(),
nb::arg("unsupported_instructions_file") = "")
nb::arg("unsupported_instructions_file") = "",
nb::arg("split_output") = false, nb::arg("output_path") = "")
.def("set_output_format", &PyRtgToolOptions::setOutputFormat,
"Specify the output format of the tool", nb::arg("format"))
.def("set_seed", &PyRtgToolOptions::setSeed,
Expand All @@ -114,6 +128,13 @@ void circt::python::populateDialectRTGToolSubmodule(nb::module_ &m) {
&PyRtgToolOptions::setUnsupportedInstructionsFile,
"Register a file containing a comma-separated list of instruction "
"names which are not supported by the assembler.",
nb::arg("filename"))
.def("set_split_output", &PyRtgToolOptions::setSplitOutput,
"Determines whether each test should be emitted to a separate file.",
nb::arg("filename"))
.def("output_path", &PyRtgToolOptions::setOutputPath,
"The path of a file to be emitted to or a directory if "
"'split_output' is enabled.",
nb::arg("filename"));

m.def("populate_randomizer_pipeline",
Expand Down
37 changes: 37 additions & 0 deletions lib/CAPI/Dialect/RTG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,40 @@ MlirType rtgDictTypeGet(MlirContext ctxt, intptr_t numEntries,
}
return wrap(DictType::get(unwrap(ctxt), entries));
}

//===----------------------------------------------------------------------===//
// Attribute API.
//===----------------------------------------------------------------------===//

bool rtgAttrIsALabelVisibilityAttr(MlirAttribute attr) {
return isa<LabelVisibilityAttr>(unwrap(attr));
}

RTGLabelVisibility rtgLabelVisibilityAttrGetValue(MlirAttribute attr) {
auto convert = [](LabelVisibility visibility) {
switch (visibility) {
case LabelVisibility::local:
return RTG_LABEL_VISIBILITY_LOCAL;
case LabelVisibility::global:
return RTG_LABEL_VISIBILITY_GLOBAL;
case LabelVisibility::external:
return RTG_LABEL_VISIBILITY_EXTERNAL;
}
};
return convert(cast<LabelVisibilityAttr>(unwrap(attr)).getValue());
}

MlirAttribute rtgLabelVisibilityAttrGet(MlirContext ctxt,
RTGLabelVisibility visibility) {
auto convert = [](RTGLabelVisibility visibility) {
switch (visibility) {
case RTG_LABEL_VISIBILITY_LOCAL:
return LabelVisibility::local;
case RTG_LABEL_VISIBILITY_GLOBAL:
return LabelVisibility::global;
case RTG_LABEL_VISIBILITY_EXTERNAL:
return LabelVisibility::external;
}
};
return wrap(LabelVisibilityAttr::get(unwrap(ctxt), convert(visibility)));
}
8 changes: 8 additions & 0 deletions lib/CAPI/Dialect/RTGTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ MlirType rtgtestCPUTypeGet(MlirContext ctxt) {
return wrap(CPUType::get(unwrap(ctxt)));
}

bool rtgtestTypeIsAIntegerRegister(MlirType type) {
return isa<IntegerRegisterType>(unwrap(type));
}

MlirType rtgtestIntegerRegisterTypeGet(MlirContext ctxt) {
return wrap(IntegerRegisterType::get(unwrap(ctxt)));
}

// Immediates.
//===----------------------------------------------------------------------===//

Expand Down
15 changes: 13 additions & 2 deletions lib/CAPI/RtgTool/RtgTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ void circtRtgToolOptionsSetVerbosePassExecution(CirctRtgToolOptions options,

void circtRtgToolOptionsSetUnsupportedInstructions(
CirctRtgToolOptions options, unsigned numInstr,
const char **unsupportedInstructions) {
const void **unsupportedInstructions) {
SmallVector<std::string> instr;
for (unsigned i = 0; i < numInstr; ++i)
instr.push_back(std::string(unsupportedInstructions[i]));
instr.push_back(
reinterpret_cast<std::string *>(unsupportedInstructions)[i]);
unwrap(options)->setUnsupportedInstructions(std::move(instr));
}

Expand All @@ -82,6 +83,16 @@ void circtRtgToolOptionsSetUnsupportedInstructionsFile(
unwrap(options)->setUnsupportedInstructionsFile(std::string(filename));
}

void circtRtgToolOptionsSetSplitOutput(CirctRtgToolOptions options,
bool enable) {
unwrap(options)->setSplitOutput(enable);
}

void circtRtgToolOptionsSetOutputPath(CirctRtgToolOptions options,
const char *path) {
unwrap(options)->setOutputPath(std::string(path));
}

//===----------------------------------------------------------------------===//
// Pipeline Population API.
//===----------------------------------------------------------------------===//
Expand Down
2 changes: 2 additions & 0 deletions lib/Tools/rtgtool/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ add_circt_library(CIRCTRtgToolLib
RtgToolOptions.cpp

LINK_LIBS PUBLIC
CIRCTRTGDialect
CIRCTRTGTransforms
CIRCTSupport

MLIRIR
Expand Down
Loading