Skip to content

Commit

Permalink
add typecache for sys integers
Browse files Browse the repository at this point in the history
  • Loading branch information
ShlKan committed Jan 12, 2025
1 parent fe18085 commit e93f794
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
25 changes: 22 additions & 3 deletions IRAnalysis/CodeGen/SysGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
#include "CIRGenModule.h"
#include "SysGenProcess.h"
#include "SysIR/Dialect/IR/SysDialect.h"
#include "SysIR/Dialect/IR/SysTypes.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/Expr.h"
#include "clang/AST/Type.h"
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
#include <cstddef>
#include <cstdint>
#include <string>
#include <utility>
Expand Down Expand Up @@ -63,14 +66,14 @@ void SysGenModule::buildSysModule(clang::CXXRecordDecl *moduleDecl) {
builder.setInsertionPointToEnd(theModule.getBody());

for (const auto &field : moduleDecl->fields()) {
auto val = field->getInClassInitializer()->EvaluateKnownConstInt(
moduleDecl->getASTContext());
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 = SInt32Ty;
builder.create<mlir::cir::ConstantOp>(
getLoc(field->getLocation()), mlir::cir::IntAttr::get(i32Ty, 2));
builder.getConstInt(getLoc(field->getLocation()), val);
break;
}
default:
Expand Down Expand Up @@ -101,6 +104,22 @@ void SysGenModule::buildSysModule(clang::CXXRecordDecl *moduleDecl) {
theModule->dump();
}

mlir::sys::SIntType SysGenModule::getSSignedIntType(uint32_t size) {
if (sSignedIntTyMap.count(size))
return sSignedIntTyMap[size];
auto ty = mlir::sys::SIntType::get(builder.getContext(), size, true);
sSignedIntTyMap[size] = ty;
return ty;
}

mlir::sys::SIntType SysGenModule::getSUSignedIntType(uint32_t size) {
if (sUnsigendIntTyMap.count(size))
return sUnsigendIntTyMap[size];
auto ty = mlir::sys::SIntType::get(builder.getContext(), size, false);
sUnsigendIntTyMap[size] = ty;
return ty;
}

SysGenModule::~SysGenModule() {}

} // namespace sys
10 changes: 8 additions & 2 deletions IRAnalysis/CodeGen/SysGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
#include "clang/AST/Expr.h"
#include "clang/Basic/Diagnostic.h"
#include "llvm/ADT/SmallVector.h"
#include <cstdint>
#include <map>

#include "CIRGenModule.h"

#include "CPPFrontend/CIROptions.h"

#include "SysIR/Dialect/IR/SysTypes.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/MLIRContext.h"
Expand All @@ -32,12 +35,15 @@ class SysGenModule : public cir::CIRGenModule {
const cir::CIROptions &CIROption,
clang::DiagnosticsEngine &Diags);

mlir::sys::SIntType getSSignedIntType(uint32_t size);
mlir::sys::SIntType getSUSignedIntType(uint32_t size);

~SysGenModule();

private:
llvm::SmallVector<clang::StringLiteral *, 4> processNames;

private:
std::map<uint32_t, mlir::sys::SIntType> sSignedIntTyMap;
std::map<uint32_t, mlir::sys::SIntType> sUnsigendIntTyMap;
void collectProcess(clang::CXXRecordDecl *moduleDecl);

public:
Expand Down
2 changes: 1 addition & 1 deletion IRAnalysis/test/SystemC/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SC_MODULE(Process) {
SC_THREAD(emptyProcess1);
}

int x;
int x = 9;
void emptyProcess() {
// Do nothing.
}
Expand Down

0 comments on commit e93f794

Please sign in to comment.