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

feat: add translation for fields #32

Merged
merged 1 commit into from
Jan 4, 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
34 changes: 32 additions & 2 deletions IRAnalysis/CodeGen/SysGenModule.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@


#include "SysGenModule.h"
#include "CIR/Dialect/IR/CIRDialect.h"
#include "CIR/Dialect/IR/CIROps.h.inc"
#include "CIR/Dialect/IR/CIRTypes.h"
#include "CIRGenBuilder.h"
#include "SysGenProcess.h"
#include "SysIR/Dialect/IR/SysDialect.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Location.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/Expr.h"
#include "clang/AST/Type.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
#include <cstdint>
#include <string>
#include <utility>


namespace sys {

SysGenModule::SysGenModule(mlir::MLIRContext &context,
Expand Down Expand Up @@ -53,6 +60,29 @@ void SysGenModule::buildSysModule(clang::CXXRecordDecl *moduleDecl) {
SysGenProcess genProcess(*this, builder);
builder.setInsertionPointToEnd(theModule.getBody());

for (const auto &field : moduleDecl->fields()) {
auto ty = astCtx.getCanonicalType(field->getType());
switch (field->getType().getTypePtr()->getTypeClass()) {
case clang::Type::Builtin: {
switch (llvm::dyn_cast<clang::BuiltinType>(ty)->getKind()) {
case clang::BuiltinType::Int: {
mlir::cir::IntType i32Ty =
mlir::cir::IntType::get(builder.getContext(), 32, true);
builder.create<mlir::cir::ConstantOp>(
getLoc(field->getLocation()), mlir::cir::IntAttr::get(i32Ty, 2));
break;
}
default:
llvm_unreachable("Unsupport builtin type.");
}
break;
}
default:
llvm_unreachable("Unsupport type.");
break;
}
}

collectProcess(moduleDecl);
llvm::SmallVector<mlir::sys::ProcDefOP, 4> processOPs;
for (const auto &method : moduleDecl->methods()) {
Expand Down
4 changes: 2 additions & 2 deletions IRAnalysis/CodeGen/SysIRGenerator.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//===--- SysIRGenerator.cpp - Emit Sys IR from ASTs ----------------------===//

#include "SysIR/SysIRGenerator.h"
#include "CIR/Dialect/IR/CIRDialect.h"
#include "SysGenModule.h"
#include "SysIR/Dialect/IR/SysDialect.h"

#include "mlir/IR/BuiltinOps.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/Basic/TokenKinds.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/raw_ostream.h"
#include <memory>

using namespace sys;
Expand All @@ -27,6 +26,7 @@ void SysIRGenerator::Initialize(clang::ASTContext &Context) {
this->astCtx = &Context;
mlirCtx = std::make_unique<mlir::MLIRContext>();
mlirCtx->getOrLoadDialect<mlir::sys::SysDialect>();
mlirCtx->getOrLoadDialect<mlir::cir::CIRDialect>();
sysMG = std::make_unique<SysGenModule>(*mlirCtx, *astCtx, cirOpts, Diags);
}

Expand Down
4 changes: 3 additions & 1 deletion IRAnalysis/CodeGen/TargetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "CIRGenFunctionInfo.h"
#include "CIRGenTypes.h"

#include "clang/Basic/TargetInfo.h"
#include "CIR/Target/x86.h"
#include "clang/Basic/TargetInfo.h"

using namespace cir;
using namespace clang;
Expand Down Expand Up @@ -608,6 +608,8 @@ const TargetCIRGenInfo &CIRGenModule::getTargetCIRGenInfo() {
assert(false && "OSType NYI");
case llvm::Triple::Linux:
return SetCIRGenInfo(new X86_64TargetCIRGenInfo(genTypes, AVXLevel));
case llvm::Triple::Darwin:
return SetCIRGenInfo(new X86_64TargetCIRGenInfo(genTypes, AVXLevel));
}
}

Expand Down
11 changes: 3 additions & 8 deletions IRAnalysis/Dialect/SysIR/SysDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "mlir/IR/OpImplementation.h"
#include "mlir/Support/LLVM.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/ErrorHandling.h"

using namespace ::mlir;
using namespace ::mlir::sys;
Expand Down Expand Up @@ -35,18 +36,12 @@ void ProcDefOP::build(::mlir::OpBuilder &odsBuilder,
Attribute SysDialect::parseAttribute(mlir::DialectAsmParser &,
mlir::Type) const {
// TODO
llvm::llvm_unreachable_internal("parseAttribute: Have not yet implemented");
}

void SysDialect::printAttribute(Attribute attr, DialectAsmPrinter &os) const {
// TODO
}

Type SysDialect::parseType(DialectAsmParser &parser) const {
// TODO
}

void SysDialect::printType(Type type, DialectAsmPrinter &printer) const {
// TODO
llvm::llvm_unreachable_internal("printAttribute: Have not yet implemented");
}

#define GET_OP_CLASSES
Expand Down
24 changes: 22 additions & 2 deletions IRAnalysis/Dialect/SysIR/SysTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
#include "SysIR/Dialect/IR/SysTypes.h"
#include "SysIR/Dialect/IR/SysDialect.h"

#include "mlir/IR/OpImplementation.h"
#include "mlir/IR/Types.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/DialectImplementation.h"
#include "mlir/Interfaces/DataLayoutInterfaces.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Support/LogicalResult.h"

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
Expand Down Expand Up @@ -98,3 +102,19 @@ SIntType::getPreferredAlignment(const ::mlir::DataLayout &dataLayout,
::mlir::DataLayoutEntryListRef params) const {
return (uint64_t)(getWidth() / 8);
}

mlir::Type SysDialect::parseType(DialectAsmParser &printer) const {
// TODO
llvm::llvm_unreachable_internal("parseType: Have not yet implemented");
}

void SysDialect::printType(Type type, mlir::DialectAsmPrinter &printer) const {
if (generatedTypePrinter(type, printer).succeeded())
return;
llvm::TypeSwitch<Type>(type)
.Case<SIntType>([&](SIntType type) { type.print(printer); })
.Case<SProcessType>([&](SProcessType type) { type.print(printer); })
.Default([&](Type) {
llvm::report_fatal_error("printer is missing a handler for this type");
});
}
1 change: 0 additions & 1 deletion IRAnalysis/FrontendAction/SysGenAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "clang/AST/DeclGroup.h"

#include <SysIRFrontendAction/SysGenAction.h>
#include <iostream>

#include <memory>

Expand Down
2 changes: 1 addition & 1 deletion IRAnalysis/include/SysIR/Dialect/IR/SysOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def ProcRegisterOP : SysOP<"ProcRegister"> {
}];

let arguments = (ins SysProcType:$proc, Variadic<Sys_AnyType>:$args);
let assemblyFormat = "$proc $args attr-dict `:` `(` type($args) `)` `=``>` type($proc)";
let assemblyFormat = "$proc $args attr-dict `[` type($proc) ` ` type($args) `]` `:` `void`";
}


Expand Down
3 changes: 1 addition & 2 deletions IRAnalysis/include/SysIR/Dialect/IR/SysTypes.td
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,9 @@ def SysProcType : Sys_Type<"SProcess", "s_proc"> {
let parameters = (ins ArrayRefParameter<"Type">:$inputs);

let assemblyFormat = [{
`P` ` ` `(` custom<ProcArgs>($inputs) `)`
`Proc` ` ` `(` custom<ProcArgs>($inputs) `)`
}];


}

def Sys_AnyType : AnyTypeOf<[
Expand Down
25 changes: 13 additions & 12 deletions IRAnalysis/test/SystemC/process.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#include <systemc>

SC_MODULE(Process){
SC_MODULE(Process) {

SC_CTOR(Process){SC_THREAD(emptyProcess);
SC_THREAD(emptyProcess1);
}
SC_CTOR(Process) {
SC_THREAD(emptyProcess);
SC_THREAD(emptyProcess1);
}

void emptyProcess(void) {
// Do nothing.
}
int x;
void emptyProcess() {
// Do nothing.
}

void emptyProcess1(void) {
// Do nothing.
}
}
;
void emptyProcess1(void) {
// Do nothing.
}
};
Loading