Skip to content

Commit fd6141e

Browse files
committed
[CIR] Gracefully fail null getIdentifier() and large _BitInt
Fail gracefully when `getIdentifier()` for a declaration is null, such for an overloaded operator. Report an error and skip the declaration. Fail gracefully when processing a large `_BitInt`, with a width greater than 128. Report an error and return `int` instead. Remove the remaining fixed-point integral types from `convertType`. Nothing can be done with them because fixed-point operations are not yet implemented in the incubator, and I am not convinced that the types are being converted correctly. A couple small fixes to comments and documentation and formatting.
1 parent 58310a1 commit fd6141e

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def GlobalOp : CIR_Op<"global"> {
9999
let skipDefaultBuilders = 1;
100100

101101
let builders = [OpBuilder<(ins "llvm::StringRef":$sym_name,
102-
"mlir::Type":$sym_type)>];
102+
"mlir::Type":$sym_type)>];
103103

104104
let hasVerifier = 1;
105105
}
@@ -115,7 +115,7 @@ def GlobalOp : CIR_Op<"global"> {
115115
def FuncOp : CIR_Op<"func"> {
116116
let summary = "Declare or define a function";
117117
let description = [{
118-
THe `cir.func` operation defines a function, similar to the `mlir::FuncOp`
118+
The `cir.func` operation defines a function, similar to the `mlir::FuncOp`
119119
built-in.
120120
}];
121121

clang/include/clang/CIR/Dialect/IR/CIRTypes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- CIRTypes.h - MLIR CIR Types ------------------------------*- C++ -*-===//
1+
//===----------------------------------------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,27 @@ void CIRGenModule::emitGlobal(clang::GlobalDecl gd) {
7878
void CIRGenModule::emitGlobalFunctionDefinition(clang::GlobalDecl gd,
7979
mlir::Operation *op) {
8080
auto const *funcDecl = cast<FunctionDecl>(gd.getDecl());
81-
auto funcOp = builder.create<cir::FuncOp>(
82-
getLoc(funcDecl->getSourceRange()), funcDecl->getIdentifier()->getName());
83-
theModule.push_back(funcOp);
81+
if (clang::IdentifierInfo *identifier = funcDecl->getIdentifier()) {
82+
auto funcOp = builder.create<cir::FuncOp>(
83+
getLoc(funcDecl->getSourceRange()), identifier->getName());
84+
theModule.push_back(funcOp);
85+
} else {
86+
errorNYI(funcDecl->getSourceRange().getBegin(),
87+
"function definition with a non-identifier for a name");
88+
}
8489
}
8590

8691
void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *vd,
8792
bool isTentative) {
8893
mlir::Type type = getTypes().convertType(vd->getType());
89-
auto varOp = builder.create<cir::GlobalOp>(
90-
getLoc(vd->getSourceRange()), vd->getIdentifier()->getName(), type);
91-
theModule.push_back(varOp);
94+
if (clang::IdentifierInfo *identifier = vd->getIdentifier()) {
95+
auto varOp = builder.create<cir::GlobalOp>(getLoc(vd->getSourceRange()),
96+
identifier->getName(), type);
97+
theModule.push_back(varOp);
98+
} else {
99+
errorNYI(vd->getSourceRange().getBegin(),
100+
"variable definition with a non-identifier for a name");
101+
}
92102
}
93103

94104
void CIRGenModule::emitGlobalDefinition(clang::GlobalDecl gd,

clang/lib/CIR/CodeGen/CIRGenTypes.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,37 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
1717
type = context.getCanonicalType(type);
1818
const Type *ty = type.getTypePtr();
1919

20+
// For types that haven't been implemented yet or are otherwise unsupported,
21+
// report an error and return 'int'.
22+
2023
mlir::Type resultType = nullptr;
2124
switch (ty->getTypeClass()) {
2225
case Type::Builtin: {
2326
switch (cast<BuiltinType>(ty)->getKind()) {
2427
// Signed types.
25-
case BuiltinType::Accum:
2628
case BuiltinType::Char_S:
27-
case BuiltinType::Fract:
2829
case BuiltinType::Int:
2930
case BuiltinType::Int128:
3031
case BuiltinType::Long:
31-
case BuiltinType::LongAccum:
32-
case BuiltinType::LongFract:
3332
case BuiltinType::LongLong:
3433
case BuiltinType::SChar:
3534
case BuiltinType::Short:
36-
case BuiltinType::ShortAccum:
37-
case BuiltinType::ShortFract:
3835
case BuiltinType::WChar_S:
3936
resultType = cir::IntType::get(cgm.getBuilder().getContext(),
4037
context.getTypeSize(ty),
4138
/*isSigned=*/true);
4239
break;
4340
// Unsigned types.
41+
case BuiltinType::Char8:
4442
case BuiltinType::Char16:
4543
case BuiltinType::Char32:
46-
case BuiltinType::Char8:
4744
case BuiltinType::Char_U:
48-
case BuiltinType::UAccum:
4945
case BuiltinType::UChar:
50-
case BuiltinType::UFract:
5146
case BuiltinType::UInt:
5247
case BuiltinType::UInt128:
5348
case BuiltinType::ULong:
54-
case BuiltinType::ULongAccum:
55-
case BuiltinType::ULongFract:
5649
case BuiltinType::ULongLong:
5750
case BuiltinType::UShort:
58-
case BuiltinType::UShortAccum:
59-
case BuiltinType::UShortFract:
6051
case BuiltinType::WChar_U:
6152
resultType = cir::IntType::get(cgm.getBuilder().getContext(),
6253
context.getTypeSize(ty),
@@ -72,9 +63,15 @@ mlir::Type CIRGenTypes::convertType(QualType type) {
7263
}
7364
case Type::BitInt: {
7465
const auto *bitIntTy = cast<BitIntType>(type);
75-
resultType =
76-
cir::IntType::get(cgm.getBuilder().getContext(), bitIntTy->getNumBits(),
77-
bitIntTy->isSigned());
66+
if (bitIntTy->getNumBits() > cir::IntType::maxBitwidth()) {
67+
cgm.errorNYI(SourceLocation(), "large _BitInt type", type);
68+
resultType = cir::IntType::get(cgm.getBuilder().getContext(), 32,
69+
/*isSigned=*/true);
70+
} else {
71+
resultType =
72+
cir::IntType::get(cgm.getBuilder().getContext(),
73+
bitIntTy->getNumBits(), bitIntTy->isSigned());
74+
}
7875
break;
7976
}
8077
default:

0 commit comments

Comments
 (0)