From d9cdb800c741094d57bf150a7d94b8c5b3fb3bf6 Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Thu, 13 Nov 2025 20:22:33 +0100 Subject: [PATCH] [CIR] Backport support for GNUNullExpr --- clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 2 +- clang/test/CIR/CodeGen/gnu-null.cpp | 28 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 clang/test/CIR/CodeGen/gnu-null.cpp diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index 2ae8c0d10703..4b1427d1b748 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -212,7 +212,7 @@ class ScalarExprEmitter : public StmtVisitor { return emitNullValue(E->getType(), CGF.getLoc(E->getSourceRange())); } mlir::Value VisitGNUNullExpr(const GNUNullExpr *E) { - llvm_unreachable("NYI"); + return emitNullValue(E->getType(), CGF.getLoc(E->getSourceRange())); } mlir::Value VisitOffsetOfExpr(OffsetOfExpr *E) { // Try folding the offsetof to a constant. diff --git a/clang/test/CIR/CodeGen/gnu-null.cpp b/clang/test/CIR/CodeGen/gnu-null.cpp new file mode 100644 index 000000000000..d1d15f200762 --- /dev/null +++ b/clang/test/CIR/CodeGen/gnu-null.cpp @@ -0,0 +1,28 @@ +// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR +// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -fclangir -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM +// RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -Wno-unused-value -emit-llvm %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s -check-prefix=OGCG + +void gnu_null_expr() { + long a = __null; + int *b = __null; +} + +// CIR: %[[A_ADDR:.*]] = cir.alloca !s64i, !cir.ptr, ["a", init] +// CIR: %[[B_ADDR:.*]] = cir.alloca !cir.ptr, !cir.ptr>, ["b", init] +// CIR: %[[CONST_0:.*]] = cir.const #cir.int<0> : !s64i +// CIR: cir.store {{.*}} %[[CONST_0]], %[[A_ADDR]] : !s64i, !cir.ptr +// CIR: %[[CONST_NULL:.*]] = cir.const #cir.ptr : !cir.ptr +// CIR: cir.store {{.*}} %[[CONST_NULL]], %[[B_ADDR]] : !cir.ptr, !cir.ptr> + +// LLVM: %[[A_ADDR:.*]] = alloca i64, i64 1, align 8 +// LLVM: %[[B_ADDR:.*]] = alloca ptr, i64 1, align 8 +// LLVM: store i64 0, ptr %[[A_ADDR]], align 8 +// LLVM: store ptr null, ptr %[[B_ADDR]], align 8 + +// OGCG: %[[A_ADDR:.*]] = alloca i64, align 8 +// OGCG: %[[B_ADDR:.*]] = alloca ptr, align 8 +// OGCG: store i64 0, ptr %[[A_ADDR]], align 8 +// OGCG: store ptr null, ptr %[[B_ADDR]], align 8