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

[RTG][RTGTest] Add CAPI and a basic lowering pipeline #7882

Open
wants to merge 1 commit into
base: maerhart-rtg-elaboration-pass
Choose a base branch
from
Open
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
54 changes: 54 additions & 0 deletions include/circt-c/Dialect/RTG.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//===- RTG.h - C interface for the for RTG dialect ----------------*- C -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_C_DIALECT_RTG_H
#define CIRCT_C_DIALECT_RTG_H

#include "mlir-c/IR.h"

#ifdef __cplusplus
extern "C" {
#endif

//===----------------------------------------------------------------------===//
// Dialect API.
//===----------------------------------------------------------------------===//

MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(RTG, rtg);
MLIR_CAPI_EXPORTED void registerRTGPasses(void);

//===----------------------------------------------------------------------===//
// Type API.
//===----------------------------------------------------------------------===//

/// If the type is an RTG sequence.
MLIR_CAPI_EXPORTED bool rtgTypeIsASequence(MlirType type);

/// Creates an RTG sequence type in the context.
MLIR_CAPI_EXPORTED MlirType rtgSequenceTypeGet(MlirContext ctxt);

/// If the type is an RTG set.
MLIR_CAPI_EXPORTED bool rtgTypeIsASet(MlirType type);

/// Creates an RTG set type in the context.
MLIR_CAPI_EXPORTED MlirType rtgSetTypeGet(MlirType elementType);

/// If the type is an RTG dict.
MLIR_CAPI_EXPORTED bool rtgTypeIsADict(MlirType type);

/// Creates an RTG dict type in the context.
MLIR_CAPI_EXPORTED MlirType rtgDictTypeGet(MlirContext ctxt,
intptr_t numEntries,
MlirAttribute const *entryNames,
MlirType const *entryTypes);

#ifdef __cplusplus
}
#endif

#endif // CIRCT_C_DIALECT_RTG_H
38 changes: 38 additions & 0 deletions include/circt-c/Dialect/RTGTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===- RTGTest.h - C interface for the for RTGTest dialect --------*- C -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_C_DIALECT_RTGTEST_H
#define CIRCT_C_DIALECT_RTGTEST_H

#include "mlir-c/IR.h"

#ifdef __cplusplus
extern "C" {
#endif

//===----------------------------------------------------------------------===//
// Dialect API.
//===----------------------------------------------------------------------===//

MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(RTGTest, rtgtest);

//===----------------------------------------------------------------------===//
// Type API.
//===----------------------------------------------------------------------===//

/// If the type is an RTGTest CPUType.
MLIR_CAPI_EXPORTED bool rtgtestTypeIsACPU(MlirType type);

/// Creates an RTGTest CPU type in the context.
MLIR_CAPI_EXPORTED MlirType rtgtestCPUTypeGet(MlirContext ctxt);

#ifdef __cplusplus
}
#endif

#endif // CIRCT_C_DIALECT_RTGTEST_H
83 changes: 83 additions & 0 deletions include/circt-c/RtgTool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//===-- circt-c/RtgTool.h - C API for the rtgtool -----------------*- C -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_C_RTGTOOL_H
#define CIRCT_C_RTGTOOL_H

#include "mlir-c/Pass.h"

#ifdef __cplusplus
extern "C" {
#endif

//===----------------------------------------------------------------------===//
// Tool Options API.
//===----------------------------------------------------------------------===//

#define DEFINE_C_API_STRUCT(name, storage) \
struct name { \
storage *ptr; \
}; \
typedef struct name name

DEFINE_C_API_STRUCT(CirctRtgToolOptions, void);

#undef DEFINE_C_API_STRUCT

// NOLINTNEXTLINE(modernize-use-using)
typedef enum CiretRtgToolOutputFormat {
CIRCT_RTGTOOL_OUTPUT_FORMAT_MLIR,
CIRCT_RTGTOOL_OUTPUT_FORMAT_ELABORATED_MLIR,
CIRCT_RTGTOOL_OUTPUT_FORMAT_ASM,
} CirctRtgToolOutputFormat;

MLIR_CAPI_EXPORTED CirctRtgToolOptions circtRtgToolOptionsCreateDefault(void);
MLIR_CAPI_EXPORTED void circtRtgToolOptionsDestroy(CirctRtgToolOptions options);

MLIR_CAPI_EXPORTED void
circtRtgToolOptionsSetOutputFormat(CirctRtgToolOptions options,
CirctRtgToolOutputFormat format);

MLIR_CAPI_EXPORTED void circtRtgToolOptionsSetSeed(CirctRtgToolOptions options,
unsigned seed);
MLIR_CAPI_EXPORTED void
circtRtgToolOptionsUnsetSeed(CirctRtgToolOptions options);

MLIR_CAPI_EXPORTED void
circtRtgToolOptionsSetVerifyPasses(CirctRtgToolOptions options, bool enable);

MLIR_CAPI_EXPORTED void
circtRtgToolOptionsSetVerbosePassExecution(CirctRtgToolOptions options,
bool enable);

MLIR_CAPI_EXPORTED void
circtRtgToolOptionsSetDebugMode(CirctRtgToolOptions options, bool enable);

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

MLIR_CAPI_EXPORTED void circtRtgToolOptionsAddUnsupportedInstruction(
CirctRtgToolOptions options, const char *unsupportedInstruction);

MLIR_CAPI_EXPORTED void
circtRtgToolOptionsSetUnsupportedInstructionsFile(CirctRtgToolOptions options,
const char *filename);

//===----------------------------------------------------------------------===//
// Pipeline Population API.
//===----------------------------------------------------------------------===//

MLIR_CAPI_EXPORTED void
circtRtgToolRandomizerPipeline(MlirPassManager pm, CirctRtgToolOptions options);

#ifdef __cplusplus
}
#endif

#endif // CIRCT_C_RTGTOOL_H
2 changes: 1 addition & 1 deletion include/circt/Dialect/RTGTest/IR/RTGTestTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ include "mlir/IR/AttrTypeBase.td"
class RTGTestTypeDef<string name, list<Trait> traits = []>
: TypeDef<RTGTestDialect, name, traits>;

def CPUType : RTGTestTypeDef<"cpu", [ContextResourceTypeInterface]> {
def CPUType : RTGTestTypeDef<"CPU", [ContextResourceTypeInterface]> {
let summary = "handle to a specific CPU";
let description = [{
This type implements a specific context resource to test RTG operations
Expand Down
107 changes: 107 additions & 0 deletions include/circt/Tools/rtgtool/RtgToolOptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//===- RtgToolOptions.h - Configuration Options for rtgtool -----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This header file defines configuration options for the rtgtool.
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_TOOLS_RTGTOOL_RTGTOOLOPTIONS_H
#define CIRCT_TOOLS_RTGTOOL_RTGTOOLOPTIONS_H

#include "circt/Support/LLVM.h"
#include "mlir/Pass/PassManager.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include <optional>

namespace circt {
namespace rtg {

/// The set of options used to control the behavior of the RTG tool.
class RtgToolOptions {
public:
enum class OutputFormat { MLIR, ElaboratedMLIR, ASM };

void setOutputFormat(OutputFormat format) { outputFormat = format; }
OutputFormat getOutputFormat() const { return outputFormat; }

RtgToolOptions &setSeed(unsigned seed) {
this->seed = seed;
return *this;
}
RtgToolOptions &unsetSeed() {
this->seed = std::nullopt;
return *this;
}
bool hasSeed() const { return seed != std::nullopt; }
unsigned getSeed() const {
assert(hasSeed());
return *seed;
}

RtgToolOptions &setVerifyPasses(bool enable) {
verifyPasses = enable;
return *this;
}
bool getVerifyPasses() const { return verifyPasses; }

RtgToolOptions &setVerbosePassExecution(bool enable) {
verbosePassExecution = enable;
return *this;
}
bool getVerbosePassExecution() const { return verbosePassExecution; }

RtgToolOptions &setDebugMode(bool enable) {
debugMode = enable;
return *this;
}
bool getDebugMode() const { return debugMode; }

RtgToolOptions &setUnsupportedInstructions(SmallVector<std::string> &&instr) {
unsupportedInstructions = instr;
return *this;
}
RtgToolOptions &setUnsupportedInstructions(ArrayRef<std::string> instr) {
unsupportedInstructions = SmallVector<std::string>(instr);
return *this;
}
RtgToolOptions &addUnsupportedInstruction(const std::string &instr) {
unsupportedInstructions.push_back(instr);
return *this;
}
ArrayRef<std::string> getUnsupportedInstructions() const {
return unsupportedInstructions;
}

RtgToolOptions &setUnsupportedInstructionsFile(StringRef filename) {
unsupportedInstructionsFile = filename;
return *this;
}
std::string getUnsupportedInstructionsFile() const {
return unsupportedInstructionsFile;
}

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

/// Populates the passes necessary to lower IR with RTG randomization operations
/// to fully elaborated IR (i.e., IR without random constructs).
void populateRandomizerPipeline(mlir::PassManager &pm,
const RtgToolOptions &options);

} // namespace rtg
} // namespace circt

#endif // CIRCT_TOOLS_RTGTOOL_RTGTOOLOPTIONS_H
1 change: 1 addition & 0 deletions lib/CAPI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ add_subdirectory(ExportFIRRTL)
add_subdirectory(ExportVerilog)
add_subdirectory(Dialect)
add_subdirectory(Firtool)
add_subdirectory(RtgTool)
21 changes: 21 additions & 0 deletions lib/CAPI/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ set(LLVM_OPTIONAL_SOURCES
MSFT.cpp
Moore.cpp
OM.cpp
RTG.cpp
RTGTest.cpp
SV.cpp
Seq.cpp
Verif.cpp
Expand Down Expand Up @@ -119,6 +121,25 @@ add_circt_public_c_api_library(CIRCTCAPIOM
CIRCTOMEvaluator
)

add_circt_public_c_api_library(CIRCTCAPIRTG
RTG.cpp

LINK_LIBS PUBLIC
MLIRCAPIIR
CIRCTRTGDialect
CIRCTRTGTransforms
)

if(CIRCT_INCLUDE_TESTS)
add_circt_public_c_api_library(CIRCTCAPIRTGTest
RTGTest.cpp

LINK_LIBS PUBLIC
MLIRCAPIIR
CIRCTRTGTestDialect
)
endif()

add_circt_public_c_api_library(CIRCTCAPISeq
Seq.cpp

Expand Down
Loading
Loading