Skip to content

Commit 73bc1df

Browse files
committed
Update on "[CIR] Add reconcile unrealized casts pass"
[ghstack-poisoned]
2 parents c8888dc + fb2afc5 commit 73bc1df

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "mlir/IR/Builders.h"
2424
#include "mlir/IR/BuiltinAttributes.h"
2525
#include "mlir/IR/BuiltinTypes.h"
26+
#include "llvm/ADT/APSInt.h"
2627
#include "llvm/ADT/FloatingPointMode.h"
2728
#include "llvm/Support/ErrorHandling.h"
2829

@@ -286,6 +287,13 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
286287
uint64_t C) {
287288
return create<mlir::cir::ConstantOp>(loc, t, mlir::cir::IntAttr::get(t, C));
288289
}
290+
mlir::cir::ConstantOp getConstInt(mlir::Location loc, llvm::APSInt intVal) {
291+
bool isSigned = intVal.isSigned();
292+
auto width = intVal.getBitWidth();
293+
mlir::cir::IntType t = isSigned ? getSIntNTy(width) : getUIntNTy(width);
294+
return getConstInt(
295+
loc, t, isSigned ? intVal.getSExtValue() : intVal.getZExtValue());
296+
}
289297
mlir::cir::ConstantOp getBool(bool state, mlir::Location loc) {
290298
return create<mlir::cir::ConstantOp>(loc, getBoolTy(),
291299
getCIRBoolAttr(state));

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,7 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
186186
llvm_unreachable("NYI");
187187
}
188188
mlir::Value VisitOffsetOfExpr(OffsetOfExpr *E) { llvm_unreachable("NYI"); }
189-
mlir::Value VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E) {
190-
llvm_unreachable("NYI");
191-
}
189+
mlir::Value VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
192190
mlir::Value VisitAddrLabelExpr(const AddrLabelExpr *E) {
193191
llvm_unreachable("NYI");
194192
}
@@ -2152,3 +2150,23 @@ mlir::Value ScalarExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
21522150

21532151
return Val;
21542152
}
2153+
2154+
/// Return the size or alignment of the type of argument of the sizeof
2155+
/// expression as an integer.
2156+
mlir::Value ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
2157+
const UnaryExprOrTypeTraitExpr *E) {
2158+
QualType TypeToSize = E->getTypeOfArgument();
2159+
if (E->getKind() == UETT_SizeOf) {
2160+
if (const VariableArrayType *VAT =
2161+
CGF.getContext().getAsVariableArrayType(TypeToSize)) {
2162+
llvm_unreachable("NYI");
2163+
}
2164+
} else if (E->getKind() == UETT_OpenMPRequiredSimdAlign) {
2165+
llvm_unreachable("NYI");
2166+
}
2167+
2168+
// If this isn't sizeof(vla), the result must be constant; use the constant
2169+
// folding logic so we don't have to duplicate it here.
2170+
return Builder.getConstInt(CGF.getLoc(E->getSourceRange()),
2171+
E->EvaluateKnownConstInt(CGF.getContext()));
2172+
}

clang/test/CIR/CodeGen/basic.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-cir %s -o %t.cir
1+
// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir-enable -emit-cir %s -o %t.cir
22
// RUN: FileCheck --input-file=%t.cir %s
33

44
int *p0() {
@@ -162,6 +162,21 @@ void x() {
162162
// CHECK: %3 = cir.const(#false) : !cir.bool
163163
// CHECK: cir.store %3, %1 : !cir.bool, cir.ptr <!cir.bool>
164164

165+
typedef unsigned long size_type;
166+
typedef unsigned long _Tp;
167+
168+
size_type max_size() {
169+
return size_type(~0) / sizeof(_Tp);
170+
}
171+
172+
// CHECK: cir.func @_Z8max_sizev()
173+
// CHECK: %0 = cir.alloca !u64i, cir.ptr <!u64i>, ["__retval"] {alignment = 8 : i64}
174+
// CHECK: %1 = cir.const(#cir.int<0> : !s32i) : !s32i
175+
// CHECK: %2 = cir.unary(not, %1) : !s32i, !s32i
176+
// CHECK: %3 = cir.cast(integral, %2 : !s32i), !u64i
177+
// CHECK: %4 = cir.const(#cir.int<8> : !u64i) : !u64i
178+
// CHECK: %5 = cir.binop(div, %3, %4) : !u64i
179+
165180
// CHECK-DAG: #[[locScope]] = loc(fused[#[[locScopeA:loc[0-9]+]], #[[locScopeB:loc[0-9]+]]])
166181
// CHECK-DAG: #[[locScopeA]] = loc("{{.*}}basic.cpp":27:3)
167182
// CHECK-DAG: #[[locScopeB]] = loc("{{.*}}basic.cpp":31:3)

0 commit comments

Comments
 (0)