Skip to content
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
1 change: 1 addition & 0 deletions include/circt/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ def ConvertMooreToCore : Pass<"convert-moore-to-core", "mlir::ModuleOp"> {
"mlir::cf::ControlFlowDialect",
"mlir::scf::SCFDialect",
"mlir::math::MathDialect",
"mlir::LLVM::LLVMDialect",
"sim::SimDialect",
"verif::VerifDialect",
];
Expand Down
4 changes: 4 additions & 0 deletions lib/Conversion/ImportVerilog/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ struct TypeVisitor {
return moore::StringType::get(context.getContext());
}

Type visit(const slang::ast::CHandleType &type) {
return moore::ChandleType::get(context.getContext());
}

/// Emit an error for all other types.
template <typename T>
Type visit(T &&node) {
Expand Down
36 changes: 35 additions & 1 deletion lib/Conversion/MooreToCore/MooreToCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/IR/BuiltinDialect.h"
Expand All @@ -30,6 +32,7 @@
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Transforms/RegionUtils.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/IR/DerivedTypes.h"

namespace circt {
#define GEN_PASS_DEF_CONVERTMOORETOCORE
Expand Down Expand Up @@ -540,6 +543,18 @@ struct VariableOpConversion : public OpConversionPattern<VariableOp> {
if (!resultType)
return rewriter.notifyMatchFailure(op.getLoc(), "invalid variable type");

// Handle CHandle types
if (isa<mlir::LLVM::LLVMPointerType>(resultType)) {
// Use converted initializer if present; otherwise synthesize null ptr.
Value init = adaptor.getInitial();
if (!init)
init = rewriter.create<mlir::LLVM::ZeroOp>(loc, resultType);

// For pointer-typed variables we produce the SSA pointer value directly.
rewriter.replaceOp(op, init);
return success();
}

// Determine the initial value of the signal.
Value init = adaptor.getInitial();
if (!init) {
Expand Down Expand Up @@ -1712,6 +1727,7 @@ static void populateLegality(ConversionTarget &target,
target.addLegalDialect<mlir::BuiltinDialect>();
target.addLegalDialect<mlir::math::MathDialect>();
target.addLegalDialect<sim::SimDialect>();
target.addLegalDialect<mlir::LLVM::LLVMDialect>();
target.addLegalDialect<verif::VerifDialect>();

target.addLegalOp<debug::ScopeOp>();
Expand Down Expand Up @@ -1798,10 +1814,28 @@ static void populateTypeConversion(TypeConverter &typeConverter) {
return hw::StructType::get(type.getContext(), fields);
});

// Conversion of CHandle to LLVMPointerType
typeConverter.addConversion([&](ChandleType type) -> std::optional<Type> {
return LLVM::LLVMPointerType::get(type.getContext());
});

// Explicitly mark LLVMPointerType as a legal target
typeConverter.addConversion(
[](LLVM::LLVMPointerType t) -> std::optional<Type> { return t; });

typeConverter.addConversion([&](RefType type) -> std::optional<Type> {
if (auto innerType = typeConverter.convertType(type.getNestedType()))
if (auto innerType = typeConverter.convertType(type.getNestedType())) {
if (hw::isHWValueType(innerType))
return hw::InOutType::get(innerType);
// TODO: There is some abstraction missing here to correctly return a
// reference of a CHandle; return an error for now.
if (isa<mlir::LLVM::LLVMPointerType>(innerType)) {
mlir::emitError(mlir::UnknownLoc::get(type.getContext()))
<< "Emission of references of LLVMPointerType is currently "
"unsupported!";
return {};
}
}
return {};
});

Expand Down
10 changes: 10 additions & 0 deletions test/Conversion/ImportVerilog/types.sv
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,13 @@ module String;
// CHECK-NEXT: %s = moore.variable : <string>
string s;
endmodule

// CHECK-LABEL: moore.module @CHandle
module CHandle;
// CHECK: %test = moore.variable : <chandle>
chandle test;
endmodule

// CHECK-LABEL: func.func private @takesCHandle(%arg0: !moore.chandle) {
function automatic void takesCHandle(chandle test);
endfunction
4 changes: 4 additions & 0 deletions test/Conversion/MooreToCore/basic.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1279,3 +1279,7 @@ func.func @SeverityToPrint() {
return
}

// CHECK-LABEL: func.func @CHandle(%arg0: !llvm.ptr)
func.func @CHandle(%arg0: !moore.chandle) {
return
}
1 change: 1 addition & 0 deletions tools/circt-opt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ target_link_libraries(circt-opt
MLIRFuncInlinerExtension
MLIRVectorDialect
MLIRIndexDialect
MLIRLLVMIRTransforms
)

export_executable_symbols_for_plugins(circt-opt)
2 changes: 2 additions & 0 deletions tools/circt-opt/circt-opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/Index/IR/IndexDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.h"
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
Expand Down Expand Up @@ -64,6 +65,7 @@ int main(int argc, char **argv) {
circt::registerAllPasses();

mlir::func::registerInlinerExtension(registry);
mlir::LLVM::registerInlinerInterface(registry);

// Register the standard passes we want.
mlir::registerCSEPass();
Expand Down
1 change: 1 addition & 0 deletions tools/circt-verilog/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(libs
MLIRSCFDialect
MLIRSCFDialect
MLIRSupport
MLIRLLVMIRTransforms
)

add_circt_tool(circt-verilog circt-verilog.cpp DEPENDS ${libs})
Expand Down
7 changes: 6 additions & 1 deletion tools/circt-verilog/circt-verilog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
#include "mlir/Dialect/Func/Extensions/InlinerExtension.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/IR/AsmState.h"
#include "mlir/IR/BuiltinOps.h"
Expand Down Expand Up @@ -611,12 +613,15 @@ int main(int argc, char **argv) {
moore::MooreDialect,
scf::SCFDialect,
seq::SeqDialect,
verif::VerifDialect
verif::VerifDialect,
mlir::LLVM::LLVMDialect
>();
// clang-format on

// Perform the actual work and use "exit" to avoid slow context teardown.
mlir::func::registerInlinerExtension(registry);
mlir::LLVM::registerInlinerInterface(registry);

MLIRContext context(registry);
exit(failed(execute(&context)));
}