From 31f4bce66a33e8f73f843dae33aa756e6ba0f99f Mon Sep 17 00:00:00 2001 From: Priyanshu3820 <10b.priyanshu@gmail.com> Date: Mon, 17 Nov 2025 10:11:52 +0530 Subject: [PATCH 1/4] CIR: Fix X86 builtin sqrt handling and cleanup max() unsigned cast --- clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp | 37 ++++++++++++---------- clang/lib/CIR/CodeGen/CIRGenCleanup.cpp | 3 +- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp index ba160373ec77e..b48a6575eddaf 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp @@ -20,17 +20,16 @@ using namespace clang; using namespace clang::CIRGen; -template +namespace { static mlir::Value emitIntrinsicCallOp(CIRGenFunction &cgf, const CallExpr *e, - const std::string &str, - const mlir::Type &resTy, - Operands &&...op) { - CIRGenBuilderTy &builder = cgf.getBuilder(); - mlir::Location location = cgf.getLoc(e->getExprLoc()); - return cir::LLVMIntrinsicCallOp::create(builder, location, - builder.getStringAttr(str), resTy, - std::forward(op)...) - .getResult(); + llvm::StringRef name, + mlir::Type resultType, + llvm::ArrayRef args = {}) { + cgf.getCIRGenModule().errorNYI( + e->getSourceRange(), + ("CIR intrinsic lowering NYI for " + name.str()).c_str()); + return {}; +} } mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID, @@ -55,9 +54,6 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID, // Find out if any arguments are required to be integer constant expressions. assert(!cir::MissingFeatures::handleBuiltinICEArguments()); - // The operands of the builtin call - llvm::SmallVector ops; - // `ICEArguments` is a bitmap indicating whether the argument at the i-th bit // is required to be a constant integer expression. unsigned iceArguments = 0; @@ -65,8 +61,10 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID, getContext().GetBuiltinType(builtinID, error, &iceArguments); assert(error == ASTContext::GE_None && "Error while getting builtin type."); - for (auto [idx, arg] : llvm::enumerate(e->arguments())) - ops.push_back(emitScalarOrConstFoldImmArg(iceArguments, idx, arg)); + llvm::SmallVector ops; + ops.reserve(e->getNumArgs()); + for (const Expr *arg : e->arguments()) + ops.push_back(emitScalarExpr(arg)); CIRGenBuilderTy &builder = getBuilder(); mlir::Type voidTy = builder.getVoidTy(); @@ -75,7 +73,7 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID, default: return {}; case X86::BI_mm_clflush: - return emitIntrinsicCallOp(*this, e, "x86.sse2.clflush", voidTy, ops[0]); + return emitIntrinsicCallOp(*this, e, "x86.sse2.clflush", voidTy, {ops[0]}); case X86::BI_mm_lfence: return emitIntrinsicCallOp(*this, e, "x86.sse2.lfence", voidTy); case X86::BI_mm_pause: @@ -643,6 +641,10 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID, case X86::BI__builtin_ia32_kunpckdi: case X86::BI__builtin_ia32_kunpcksi: case X86::BI__builtin_ia32_kunpckhi: + cgm.errorNYI(e->getSourceRange(), + std::string("unimplemented X86 builtin call: ") + + getContext().BuiltinInfo.getName(builtinID)); + return {}; case X86::BI__builtin_ia32_sqrtsh_round_mask: case X86::BI__builtin_ia32_sqrtsd_round_mask: case X86::BI__builtin_ia32_sqrtss_round_mask: @@ -650,6 +652,9 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID, case X86::BI__builtin_ia32_sqrtpd: case X86::BI__builtin_ia32_sqrtps256: case X86::BI__builtin_ia32_sqrtps: + cgm.errorNYI(e->getSourceRange(), + "CIR lowering for x86 sqrt builtins is not implemented yet"); + return {}; case X86::BI__builtin_ia32_sqrtph256: case X86::BI__builtin_ia32_sqrtph: case X86::BI__builtin_ia32_sqrtph512: diff --git a/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp b/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp index 33f2de9137514..9bfdcea5ba275 100644 --- a/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenCleanup.cpp @@ -21,6 +21,7 @@ #include "clang/CIR/MissingFeatures.h" +#include using namespace clang; using namespace clang::CIRGen; @@ -97,7 +98,7 @@ EHScopeStack::getInnermostActiveNormalCleanup() const { char *EHScopeStack::allocate(size_t size) { size = llvm::alignTo(size, ScopeStackAlignment); if (!startOfBuffer) { - unsigned capacity = llvm::PowerOf2Ceil(std::max(size, 1024ul)); + unsigned capacity = llvm::PowerOf2Ceil(std::max(static_cast(size), 1024ul)); startOfBuffer = std::make_unique(capacity); startOfData = endOfBuffer = startOfBuffer.get() + capacity; } else if (static_cast(startOfData - startOfBuffer.get()) < size) { From 7f3f249ac097af887b872660d95242c3bf0ab0ae Mon Sep 17 00:00:00 2001 From: Priyanshu3820 <10b.priyanshu@gmail.com> Date: Mon, 17 Nov 2025 11:06:59 +0530 Subject: [PATCH 2/4] CIR: Add x86 sqrt builtin test --- clang/test/CIR/CodeGen/X86/builtin-x86-sqrt.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 clang/test/CIR/CodeGen/X86/builtin-x86-sqrt.c diff --git a/clang/test/CIR/CodeGen/X86/builtin-x86-sqrt.c b/clang/test/CIR/CodeGen/X86/builtin-x86-sqrt.c new file mode 100644 index 0000000000000..3692643c219e4 --- /dev/null +++ b/clang/test/CIR/CodeGen/X86/builtin-x86-sqrt.c @@ -0,0 +1,14 @@ +// RUN: not %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir -o - %s 2>&1 | FileCheck %s + +// Minimal stand-in for the SSE vector type. +typedef float __m128 __attribute__((__vector_size__(16))); + +// Declare the builtin explicitly so we don't need headers. +__m128 __builtin_ia32_sqrtps(__m128); + +__m128 test_sqrtps(__m128 a) { + return __builtin_ia32_sqrtps(a); +} + +// CHECK: error: ClangIR code gen Not Yet Implemented: CIR lowering for x86 sqrt builtins is not implemented yet +// CHECK: error: ClangIR code gen Not Yet Implemented: unimplemented builtin call: __builtin_ia32_sqrtps From f4139a9aafc3bd4f0c41ed2902dcb697494e2d4b Mon Sep 17 00:00:00 2001 From: Priyanshu3820 <10b.priyanshu@gmail.com> Date: Tue, 18 Nov 2025 09:06:54 +0530 Subject: [PATCH 3/4] CIR: Narrow sqrt builtin NYI changes --- clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp | 37 ++++++++++------------ 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp index b48a6575eddaf..ba160373ec77e 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp @@ -20,16 +20,17 @@ using namespace clang; using namespace clang::CIRGen; -namespace { +template static mlir::Value emitIntrinsicCallOp(CIRGenFunction &cgf, const CallExpr *e, - llvm::StringRef name, - mlir::Type resultType, - llvm::ArrayRef args = {}) { - cgf.getCIRGenModule().errorNYI( - e->getSourceRange(), - ("CIR intrinsic lowering NYI for " + name.str()).c_str()); - return {}; -} + const std::string &str, + const mlir::Type &resTy, + Operands &&...op) { + CIRGenBuilderTy &builder = cgf.getBuilder(); + mlir::Location location = cgf.getLoc(e->getExprLoc()); + return cir::LLVMIntrinsicCallOp::create(builder, location, + builder.getStringAttr(str), resTy, + std::forward(op)...) + .getResult(); } mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID, @@ -54,6 +55,9 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID, // Find out if any arguments are required to be integer constant expressions. assert(!cir::MissingFeatures::handleBuiltinICEArguments()); + // The operands of the builtin call + llvm::SmallVector ops; + // `ICEArguments` is a bitmap indicating whether the argument at the i-th bit // is required to be a constant integer expression. unsigned iceArguments = 0; @@ -61,10 +65,8 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID, getContext().GetBuiltinType(builtinID, error, &iceArguments); assert(error == ASTContext::GE_None && "Error while getting builtin type."); - llvm::SmallVector ops; - ops.reserve(e->getNumArgs()); - for (const Expr *arg : e->arguments()) - ops.push_back(emitScalarExpr(arg)); + for (auto [idx, arg] : llvm::enumerate(e->arguments())) + ops.push_back(emitScalarOrConstFoldImmArg(iceArguments, idx, arg)); CIRGenBuilderTy &builder = getBuilder(); mlir::Type voidTy = builder.getVoidTy(); @@ -73,7 +75,7 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID, default: return {}; case X86::BI_mm_clflush: - return emitIntrinsicCallOp(*this, e, "x86.sse2.clflush", voidTy, {ops[0]}); + return emitIntrinsicCallOp(*this, e, "x86.sse2.clflush", voidTy, ops[0]); case X86::BI_mm_lfence: return emitIntrinsicCallOp(*this, e, "x86.sse2.lfence", voidTy); case X86::BI_mm_pause: @@ -641,10 +643,6 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID, case X86::BI__builtin_ia32_kunpckdi: case X86::BI__builtin_ia32_kunpcksi: case X86::BI__builtin_ia32_kunpckhi: - cgm.errorNYI(e->getSourceRange(), - std::string("unimplemented X86 builtin call: ") + - getContext().BuiltinInfo.getName(builtinID)); - return {}; case X86::BI__builtin_ia32_sqrtsh_round_mask: case X86::BI__builtin_ia32_sqrtsd_round_mask: case X86::BI__builtin_ia32_sqrtss_round_mask: @@ -652,9 +650,6 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID, case X86::BI__builtin_ia32_sqrtpd: case X86::BI__builtin_ia32_sqrtps256: case X86::BI__builtin_ia32_sqrtps: - cgm.errorNYI(e->getSourceRange(), - "CIR lowering for x86 sqrt builtins is not implemented yet"); - return {}; case X86::BI__builtin_ia32_sqrtph256: case X86::BI__builtin_ia32_sqrtph: case X86::BI__builtin_ia32_sqrtph512: From 04987492745bbd8cdbe2c4273dc3035d3c22e791 Mon Sep 17 00:00:00 2001 From: Priyanshu3820 <10b.priyanshu@gmail.com> Date: Tue, 18 Nov 2025 09:46:25 +0530 Subject: [PATCH 4/4] CIR: Add NYI diagnostic for __builtin_ia32_sqrtps --- clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp index ba160373ec77e..0fa7511103879 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp @@ -650,6 +650,10 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID, case X86::BI__builtin_ia32_sqrtpd: case X86::BI__builtin_ia32_sqrtps256: case X86::BI__builtin_ia32_sqrtps: + CGF.CGM.getDiags().Report(E->getExprLoc(), diag::err_cir_nyi) + << "__builtin_ia32_sqrtps"; + return CIRGenFunction::IgnoreValue(); + case X86::BI__builtin_ia32_sqrtph256: case X86::BI__builtin_ia32_sqrtph: case X86::BI__builtin_ia32_sqrtph512: