Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
40 changes: 40 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {

mlir::Value VisitCastExpr(CastExpr *E);

mlir::Value VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *e);

/// Emit a conversion from the specified type to the specified destination
/// type, both of which are CIR scalar types.
/// TODO: do we need ScalarConversionOpts here? Should be done in another
Expand Down Expand Up @@ -148,3 +150,41 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
}
return {};
}

/// Return the size or alignment of the type of argument of the sizeof
/// expression as an integer.
mlir::Value ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
const UnaryExprOrTypeTraitExpr *e) {
const QualType typeToSize = e->getTypeOfArgument();
const mlir::Location loc = cgf.getLoc(e->getSourceRange());
if (auto kind = e->getKind();
kind == UETT_SizeOf || kind == UETT_DataSizeOf) {
if (const VariableArrayType *variableArrTy =
cgf.getContext().getAsVariableArrayType(typeToSize)) {
cgf.getCIRGenModule().errorNYI(e->getSourceRange(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this recovering? Will there be a known-const-int in this case to give a valid value? Same on 169.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will check EvaluateKnownConstInt and create a dummy value for recovering

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think instead that after each of these errorNYI diagnostics, we are better just returning a constant value that is reasonably sensible. A 1 or a ptr-size both seem reasonable to me.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice i used 1 as recovering value for unimplemented yet cases

"sizeof operator for VariableArrayType",
e->getStmtClassName());
return builder.getConstant(
loc, builder.getAttr<cir::IntAttr>(
cgf.cgm.UInt64Ty, llvm::APSInt(llvm::APInt(64, 1), true)));
}
} else if (e->getKind() == UETT_OpenMPRequiredSimdAlign) {
cgf.getCIRGenModule().errorNYI(
e->getSourceRange(), "sizeof operator for OpenMpRequiredSimdAlign",
e->getStmtClassName());
return builder.getConstant(
loc, builder.getAttr<cir::IntAttr>(
cgf.cgm.UInt64Ty, llvm::APSInt(llvm::APInt(64, 1), true)));
} else if (e->getKind() == UETT_VectorElements) {
cgf.getCIRGenModule().errorNYI(e->getSourceRange(),
"sizeof operator for VectorElements",
e->getStmtClassName());
return builder.getConstant(
loc, builder.getAttr<cir::IntAttr>(
cgf.cgm.UInt64Ty, llvm::APSInt(llvm::APInt(64, 1), true)));
}

return builder.getConstant(
loc, builder.getAttr<cir::IntAttr>(
cgf.cgm.UInt64Ty, e->EvaluateKnownConstInt(cgf.getContext())));
}
35 changes: 35 additions & 0 deletions clang/test/CIR/CodeGen/unary-expr-or-type-trait.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o - 2>&1 | filecheck %s

void foo() {
unsigned long b = sizeof(bool);
// CHECK: cir.const #cir.int<1> : !cir.int<u, 64>

unsigned long i = sizeof(int);
// CHECK: cir.const #cir.int<4> : !cir.int<u, 64>

unsigned long l = sizeof(long);
// CHECK: cir.const #cir.int<8> : !cir.int<u, 64>

unsigned long f = sizeof(float);
// CHECK: cir.const #cir.int<4> : !cir.int<u, 64>

unsigned long d = sizeof(double);
// CHECK: cir.const #cir.int<8> : !cir.int<u, 64>
}

void foo2() {
unsigned long b = alignof(bool);
// CHECK: cir.const #cir.int<1> : !cir.int<u, 64>

unsigned long i = alignof(int);
// CHECK: cir.const #cir.int<4> : !cir.int<u, 64>

unsigned long l = alignof(long);
// CHECK: cir.const #cir.int<8> : !cir.int<u, 64>

unsigned long f = alignof(float);
// CHECK: cir.const #cir.int<4> : !cir.int<u, 64>

unsigned long d = alignof(double);
// CHECK: cir.const #cir.int<8> : !cir.int<u, 64>
}
35 changes: 35 additions & 0 deletions clang/test/CIR/Lowering/unary-expr-or-type-trait.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o - 2>&1 | FileCheck %s

void foo() {
unsigned long b = sizeof(bool);
// CHECK: store i64 1, ptr {{%.*}}, align 4

unsigned long i = sizeof(int);
// CHECK: store i64 4, ptr {{%.*}}, align 4

unsigned long l = sizeof(long);
// CHECK: store i64 8, ptr {{%.*}}, align 4

unsigned long f = sizeof(float);
// CHECK: store i64 4, ptr {{%.*}}, align 4

unsigned long d = sizeof(double);
// CHECK: store i64 8, ptr {{%.*}}, align 4
}

void foo2() {
unsigned long b = alignof(bool);
// CHECK: store i64 1, ptr {{%.*}}, align 4

unsigned long i = alignof(int);
// CHECK: store i64 4, ptr {{%.*}}, align 4

unsigned long l = alignof(long);
// CHECK: store i64 8, ptr {{%.*}}, align 4

unsigned long f = alignof(float);
// CHECK: store i64 4, ptr {{%.*}}, align 4

unsigned long d = alignof(double);
// CHECK: store i64 8, ptr {{%.*}}, align 4
}