-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[CIR] Infrastructure: class CIRGenBuilderTy; cache CIR types #119037
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 LLVM_CLANG_LIB_CIRBASEBUILDER_H | ||
| #define LLVM_CLANG_LIB_CIRBASEBUILDER_H | ||
|
|
||
| #include "mlir/IR/Builders.h" | ||
|
|
||
| namespace cir { | ||
|
|
||
| class CIRBaseBuilderTy : public mlir::OpBuilder { | ||
|
|
||
| public: | ||
| CIRBaseBuilderTy(mlir::MLIRContext &mlirContext) | ||
| : mlir::OpBuilder(&mlirContext) {} | ||
| }; | ||
|
|
||
| } // namespace cir | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 LLVM_CLANG_LIB_CIR_CIRGENBUILDER_H | ||
| #define LLVM_CLANG_LIB_CIR_CIRGENBUILDER_H | ||
lanza marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #include "CIRGenTypeCache.h" | ||
|
|
||
| #include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h" | ||
|
|
||
| namespace clang::CIRGen { | ||
|
|
||
| class CIRGenBuilderTy : public cir::CIRBaseBuilderTy { | ||
| const CIRGenTypeCache &typeCache; | ||
|
|
||
| public: | ||
| CIRGenBuilderTy(mlir::MLIRContext &mlirContext, const CIRGenTypeCache &tc) | ||
| : CIRBaseBuilderTy(mlirContext), typeCache(tc) {} | ||
| }; | ||
|
|
||
| } // namespace clang::CIRGen | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,22 +9,32 @@ using namespace clang; | |
| using namespace clang::CIRGen; | ||
|
|
||
| CIRGenTypes::CIRGenTypes(CIRGenModule &genModule) | ||
| : cgm(genModule), context(genModule.getASTContext()) {} | ||
| : cgm(genModule), context(genModule.getASTContext()), | ||
| builder(cgm.getBuilder()) {} | ||
|
|
||
| CIRGenTypes::~CIRGenTypes() {} | ||
|
|
||
| mlir::MLIRContext &CIRGenTypes::getMLIRContext() const { | ||
| return *builder.getContext(); | ||
| } | ||
|
|
||
| mlir::Type CIRGenTypes::convertType(QualType type) { | ||
| type = context.getCanonicalType(type); | ||
| const Type *ty = type.getTypePtr(); | ||
|
|
||
| // Has the type already been processed? | ||
| TypeCacheTy::iterator tci = typeCache.find(ty); | ||
| if (tci != typeCache.end()) | ||
| return tci->second; | ||
|
|
||
| // For types that haven't been implemented yet or are otherwise unsupported, | ||
| // report an error and return 'int'. | ||
|
|
||
| mlir::Type resultType = nullptr; | ||
| switch (ty->getTypeClass()) { | ||
| case Type::Builtin: { | ||
| switch (cast<BuiltinType>(ty)->getKind()) { | ||
| // Signed types. | ||
| // Signed integral types. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps some additional complexity, but was it considered to use the cached kinds for all of these instead of creating it again?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought about it briefly, but decided that the code complexity was not worth the benefit. The |
||
| case BuiltinType::Char_S: | ||
| case BuiltinType::Int: | ||
| case BuiltinType::Int128: | ||
|
|
@@ -33,11 +43,10 @@ mlir::Type CIRGenTypes::convertType(QualType type) { | |
| case BuiltinType::SChar: | ||
| case BuiltinType::Short: | ||
| case BuiltinType::WChar_S: | ||
| resultType = cir::IntType::get(cgm.getBuilder().getContext(), | ||
| context.getTypeSize(ty), | ||
| resultType = cir::IntType::get(&getMLIRContext(), context.getTypeSize(ty), | ||
| /*isSigned=*/true); | ||
| break; | ||
| // Unsigned types. | ||
| // Unsigned integral types. | ||
| case BuiltinType::Char8: | ||
| case BuiltinType::Char16: | ||
| case BuiltinType::Char32: | ||
|
|
@@ -49,14 +58,12 @@ mlir::Type CIRGenTypes::convertType(QualType type) { | |
| case BuiltinType::ULongLong: | ||
| case BuiltinType::UShort: | ||
| case BuiltinType::WChar_U: | ||
| resultType = cir::IntType::get(cgm.getBuilder().getContext(), | ||
| context.getTypeSize(ty), | ||
| resultType = cir::IntType::get(&getMLIRContext(), context.getTypeSize(ty), | ||
| /*isSigned=*/false); | ||
| break; | ||
| default: | ||
| cgm.errorNYI(SourceLocation(), "processing of built-in type", type); | ||
| resultType = cir::IntType::get(cgm.getBuilder().getContext(), 32, | ||
| /*isSigned=*/true); | ||
| resultType = cgm.SInt32Ty; | ||
| break; | ||
| } | ||
| break; | ||
|
|
@@ -65,23 +72,21 @@ mlir::Type CIRGenTypes::convertType(QualType type) { | |
| const auto *bitIntTy = cast<BitIntType>(type); | ||
| if (bitIntTy->getNumBits() > cir::IntType::maxBitwidth()) { | ||
| cgm.errorNYI(SourceLocation(), "large _BitInt type", type); | ||
| resultType = cir::IntType::get(cgm.getBuilder().getContext(), 32, | ||
| /*isSigned=*/true); | ||
| resultType = cgm.SInt32Ty; | ||
| } else { | ||
| resultType = | ||
| cir::IntType::get(cgm.getBuilder().getContext(), | ||
| bitIntTy->getNumBits(), bitIntTy->isSigned()); | ||
| resultType = cir::IntType::get(&getMLIRContext(), bitIntTy->getNumBits(), | ||
| bitIntTy->isSigned()); | ||
| } | ||
| break; | ||
| } | ||
| default: | ||
| cgm.errorNYI(SourceLocation(), "processing of type", type); | ||
| resultType = | ||
| cir::IntType::get(cgm.getBuilder().getContext(), 32, /*isSigned=*/true); | ||
| resultType = cgm.SInt32Ty; | ||
| break; | ||
| } | ||
|
|
||
| assert(resultType && "Type conversion not yet implemented"); | ||
|
|
||
| typeCache[ty] = resultType; | ||
| return resultType; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.