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 Python Bindings #7883

Merged
merged 2 commits into from
Dec 4, 2024
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
53 changes: 53 additions & 0 deletions include/circt-c/Dialect/RTG.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//===- 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);

//===----------------------------------------------------------------------===//
// 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
79 changes: 79 additions & 0 deletions include/circt-c/RtgTool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//===-- 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(unsigned seed);
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
circtRtgToolOptionsSetVerifyPasses(CirctRtgToolOptions options, bool enable);

MLIR_CAPI_EXPORTED void
circtRtgToolOptionsSetVerbosePassExecution(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
1 change: 0 additions & 1 deletion include/circt/Dialect/RTGTest/IR/RTGTestOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class RTGTestOp<string mnemonic, list<Trait> traits = []> :

def CPUDeclOp : RTGTestOp<"cpu_decl", [
Pure,
ConstantLike,
uenoku marked this conversation as resolved.
Show resolved Hide resolved
ContextResourceDefining,
]> {
let summary = "declare a CPU";
Expand Down
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
93 changes: 93 additions & 0 deletions include/circt/Tools/rtgtool/RtgToolOptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//===- 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"

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 };

RtgToolOptions(unsigned seed) : seed(seed) {}

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

RtgToolOptions &setSeed(unsigned seed) {
this->seed = seed;
return *this;
}
unsigned getSeed() const { 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 &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;
unsigned seed;
bool verifyPasses = true;
bool verbosePassExecution = 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
79 changes: 79 additions & 0 deletions integration_test/Bindings/Python/dialects/rtg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# REQUIRES: bindings_python
# RUN: %PYTHON% %s | FileCheck %s

import circt

from circt.dialects import rtg, rtgtest
uenoku marked this conversation as resolved.
Show resolved Hide resolved
from circt.ir import Context, Location, Module, InsertionPoint, Block, StringAttr, TypeAttr
from circt.passmanager import PassManager
from circt import rtgtool_support as rtgtool

with Context() as ctx, Location.unknown():
circt.register_dialects(ctx)
m = Module.create()
with InsertionPoint(m.body):
cpuTy = rtgtest.CPUType.get()
dictTy = rtg.DictType.get(ctx, [(StringAttr.get('cpu0'), cpuTy),
(StringAttr.get('cpu1'), cpuTy)])

target = rtg.TargetOp('target_name', TypeAttr.get(dictTy))
targetBlock = Block.create_at_start(target.bodyRegion, [])
with InsertionPoint(targetBlock):
cpu0 = rtgtest.CPUDeclOp(cpuTy, 0)
cpu1 = rtgtest.CPUDeclOp(cpuTy, 1)
rtg.YieldOp([cpu0, cpu1])

test = rtg.TestOp('test_name', TypeAttr.get(dictTy))
Block.create_at_start(test.bodyRegion, [cpuTy, cpuTy])

# CHECK: rtg.target @target_name : !rtg.dict<cpu0: !rtgtest.cpu, cpu1: !rtgtest.cpu> {
# CHECK: [[V0:%.+]] = rtgtest.cpu_decl 0
# CHECK: [[V1:%.+]] = rtgtest.cpu_decl 1
# CHECK: rtg.yield [[V0]], [[V1]] : !rtgtest.cpu, !rtgtest.cpu
# CHECK: }
# CHECK: rtg.test @test_name : !rtg.dict<cpu0: !rtgtest.cpu, cpu1: !rtgtest.cpu> {
# CHECK: ^bb{{.*}}(%{{.*}}: !rtgtest.cpu, %{{.*}}: !rtgtest.cpu):
# CHECK: }
print(m)

with Context() as ctx, Location.unknown():
circt.register_dialects(ctx)
m = Module.create()
with InsertionPoint(m.body):
seq = rtg.SequenceOp('seq')
setTy = rtg.SetType.get(rtg.SequenceType.get())
seqBlock = Block.create_at_start(seq.bodyRegion, [setTy])

# CHECK: rtg.sequence @seq {
# CHECK: ^bb{{.*}}(%{{.*}}: !rtg.set<!rtg.sequence>):
# CHECK: }
print(m)

with Context() as ctx, Location.unknown():
circt.register_dialects(ctx)
m = Module.create()
with InsertionPoint(m.body):
seq = rtg.SequenceOp('sequence_name')
Block.create_at_start(seq.bodyRegion, [])

test = rtg.TestOp('test_name', TypeAttr.get(rtg.DictType.get()))
block = Block.create_at_start(test.bodyRegion, [])
with InsertionPoint(block):
seq_closure = rtg.SequenceClosureOp('sequence_name', [])

# CHECK: rtg.test @test_name : !rtg.dict<> {
# CHECK-NEXT: rtg.sequence_closure
# CHECK-NEXT: }
print(m)

pm = PassManager()
options = rtgtool.Options(
seed=0,
output_format=rtgtool.OutputFormat.ELABORATED_MLIR,
)
rtgtool.populate_randomizer_pipeline(pm, options)
pm.run(m.operation)

# CHECK: rtg.test @test_name : !rtg.dict<> {
# CHECK-NEXT: }
print(m)
Loading
Loading