Skip to content
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
627bcb3
Add CIR sqrt builtin support for X86
Priyanshu3820 Nov 26, 2025
d095f5a
Merge branch 'main' into cir-sqrtps-impl
Priyanshu3820 Nov 26, 2025
8c13c6f
Merge branch 'main' into cir-sqrtps-impl
Priyanshu3820 Nov 27, 2025
17a3e66
Merge branch 'main' into cir-sqrtps-impl
Priyanshu3820 Nov 27, 2025
01bb815
Merge branch 'main' into cir-sqrtps-impl
Priyanshu3820 Nov 28, 2025
4a39fd7
Implement sqrt builtins for all vector sizes
Priyanshu3820 Nov 29, 2025
ef3fd97
Test file renamed
Priyanshu3820 Nov 30, 2025
9705673
Add sqrt changes patch
Priyanshu3820 Dec 2, 2025
21119e5
group with other floating point ops
Priyanshu3820 Dec 3, 2025
90878ec
place the implementation with other floating point ops
Priyanshu3820 Dec 3, 2025
3529f40
Update clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Priyanshu3820 Dec 3, 2025
92d0ac3
Update clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
Priyanshu3820 Dec 3, 2025
0385662
Update clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Priyanshu3820 Dec 3, 2025
ddcb7b8
update clang\lib\CIR\Lowering\DirectToLLVM\LowerToLLVM.cpp
Priyanshu3820 Dec 3, 2025
1e846e7
update clang\lib\CIR\Lowering\DirectToLLVM\LowerToLLVM.cpp
Priyanshu3820 Dec 3, 2025
233efad
Update clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h
Priyanshu3820 Dec 3, 2025
9d940bc
Update clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Priyanshu3820 Dec 3, 2025
51bbcca
Remove BOM character
Priyanshu3820 Dec 4, 2025
f901f03
Merge branch 'cir-sqrtps-impl' of https://github.com/Priyanshu3820/ll…
Priyanshu3820 Dec 4, 2025
e5789b6
Apply suggestion from @Copilot
Priyanshu3820 Dec 4, 2025
8937b12
Apply suggestion from @andykaylor
Priyanshu3820 Dec 4, 2025
8a02c50
add description
Priyanshu3820 Dec 4, 2025
9923a62
Resolve merge conflict in LowerToLLVM.cpp
Priyanshu3820 Dec 4, 2025
82a9395
Remove undefined sqrt builtin cases
Priyanshu3820 Dec 4, 2025
6bd3282
Remove unused getLLVMIntrinsicNameForType function
Priyanshu3820 Dec 4, 2025
8232ce8
Removed braces
Priyanshu3820 Dec 4, 2025
bc8e4cc
Update clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Priyanshu3820 Dec 4, 2025
9284761
update clang\lib\CIR\Lowering\DirectToLLVM\LowerToLLVM.h
Priyanshu3820 Dec 4, 2025
8647b5c
Update clang/test/CIR/CodeGen/X86/cir-sqrt-builtins.c
Priyanshu3820 Dec 4, 2025
4bac65a
Update test
Priyanshu3820 Dec 4, 2025
b1ff2ab
update clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Priyanshu3820 Dec 4, 2025
8843006
Remove unused include
Priyanshu3820 Dec 5, 2025
ed82423
Move sqrt lowering with other floating point operations
Priyanshu3820 Dec 5, 2025
9e8bec2
Merge branch 'main' into cir-sqrtps-impl
Priyanshu3820 Dec 5, 2025
961c9f9
Remove BOM character
Priyanshu3820 Dec 6, 2025
e5d1a6d
gergeebranch 'cir-sqrtps-impl' of https://github.com/Priyanshu3820/ll…
Priyanshu3820 Dec 6, 2025
44ddd79
Merge branch 'main' into cir-sqrtps-impl
Priyanshu3820 Dec 6, 2025
4dd8aa0
Delete my-sqrt-changes.patch
Priyanshu3820 Dec 6, 2025
cc5ffa1
Update errorNYI call
Priyanshu3820 Dec 7, 2025
6d43c43
Merge branch 'cir-sqrtps-impl' of https://github.com/Priyanshu3820/ll…
Priyanshu3820 Dec 7, 2025
47f9b2f
update clang\lib\CIR\Lowering\DirectToLLVM\LowerToLLVM.cpp
Priyanshu3820 Dec 7, 2025
15f1f4f
update clang/test/CIR/CodeGen/X86/cir-sqrt-builtins.c
Priyanshu3820 Dec 7, 2025
b12779a
Fix line endings in CIRGenBuiltinX86.cpp
Priyanshu3820 Dec 8, 2025
996b7e7
Fix formatting
andykaylor Dec 8, 2025
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
21 changes: 21 additions & 0 deletions clang/include/clang/CIR/Dialect/IR/CIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -4677,6 +4677,27 @@ class CIR_UnaryFPToFPBuiltinOp<string mnemonic, string llvmOpName>
let llvmOp = llvmOpName;
}

def CIR_SqrtOp : CIR_UnaryFPToFPBuiltinOp<"sqrt", "SqrtOp"> {
let summary = "Floating-point square root operation";

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a description with an example of the CIR format?

We're trying to standardize the descriptions of all our operations to include an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had done that initially but removed the example after seeing other floating point ops didn't have examples too. Added it again now.

let description = [{
Computes the square root of a floating-point value or vector.

The input must be either:
• a floating-point scalar type, or
• a vector whose element type is floating-point.

The result type must match the input type exactly.

Examples:
// scalar
%r = cir.sqrt %x : !cir.fp64

// vector
%v = cir.sqrt %vec : !cir.vector<!cir.fp32 x 4>
}];
}

def CIR_ACosOp : CIR_UnaryFPToFPBuiltinOp<"acos", "ACosOp"> {
let summary = "Computes the arcus cosine of the specified value";
let description = [{
Expand Down
12 changes: 7 additions & 5 deletions clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1347,13 +1347,15 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID,
case X86::BI__builtin_ia32_sqrtsh_round_mask:
case X86::BI__builtin_ia32_sqrtsd_round_mask:
case X86::BI__builtin_ia32_sqrtss_round_mask:
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to insert a call to errorNYI here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

May I just move the sqrt implementation to the top? That way, the builtins that are currently above them will just fall through to the NYI error at the end.

Copy link
Contributor

Choose a reason for hiding this comment

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

No. We are attempting to keep builtins in the same relative order as they appear in classic codegen and the incubator in order to make the code easier to navigate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Understood!

errorNYI("Unimplemented builtin");
return {};
case X86::BI__builtin_ia32_sqrtph512:
case X86::BI__builtin_ia32_sqrtps512:
case X86::BI__builtin_ia32_sqrtpd512:
cgm.errorNYI(expr->getSourceRange(),
std::string("unimplemented X86 builtin call: ") +
getContext().BuiltinInfo.getName(builtinID));
return {};
case X86::BI__builtin_ia32_sqrtpd512: {
mlir::Location loc = getLoc(expr->getExprLoc());
mlir::Value arg = ops[0];
return cir::SqrtOp::create(builder, loc, arg.getType(), arg).getResult();
}
case X86::BI__builtin_ia32_pmuludq128:
case X86::BI__builtin_ia32_pmuludq256:
case X86::BI__builtin_ia32_pmuludq512: {
Expand Down
10 changes: 9 additions & 1 deletion clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//====- LowerToLLVM.cpp - Lowering from CIR to LLVMIR ---------------------===//
//====- LowerToLLVM.cpp - Lowering from CIR to LLVMIR ---------------------===//
Copy link
Contributor

Choose a reason for hiding this comment

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

What changed on this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

seems like a phantom diff caused due to line ending differences.

Copy link
Contributor

Choose a reason for hiding this comment

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

The line endings may be automatically updated to match the repository settings when the changes are committed, but it's a good idea to avoid changing line ending styles. There are a few lit tests where the line ending kind is actually important.

Copy link
Contributor

Choose a reason for hiding this comment

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

You still need to remove whatever BOM character is on this line.

//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand Down Expand Up @@ -186,6 +186,14 @@ mlir::LogicalResult CIRToLLVMCopyOpLowering::matchAndRewrite(
return mlir::success();
}

mlir::LogicalResult SqrtOpLowering::matchAndRewrite(
cir::SqrtOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
mlir::Type resTy = typeConverter->convertType(op.getType());
rewriter.replaceOpWithNewOp<mlir::LLVM::SqrtOp>(op, resTy, adaptor.getSrc());
return mlir::success();
}

mlir::LogicalResult CIRToLLVMCosOpLowering::matchAndRewrite(
cir::CosOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const {
Expand Down
45 changes: 45 additions & 0 deletions clang/test/CIR/CodeGen/X86/cir-sqrt-builtins.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <immintrin.h>
// Test X86-specific sqrt builtins

// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s

// Test __builtin_ia32_sqrtph512
__m512h test_sqrtph512(__m512h a) {
return __builtin_ia32_sqrtph512(a);
}
// CIR: cir.func @test_sqrtph512
// CIR: [[RES:%.*]] = cir.sqrt {{%.*}} : !cir.vector<!cir.fp16 x 32>
// CIR: cir.return [[RES]]
// LLVM: define {{.*}} @test_sqrtph512
// LLVM: call <32 x half> @llvm.sqrt.v32f16
// OGCG: define {{.*}} @test_sqrtph512
// OGCG: call <32 x half> @llvm.sqrt.v32f16

// Test __builtin_ia32_sqrtps512
__m512 test_sqrtps512(__m512 a) {
return __builtin_ia32_sqrtps512(a);
}
// CIR: cir.func @test_sqrtps512
// CIR: [[RES:%.*]] = cir.sqrt {{%.*}} : !cir.vector<!cir.float x 16>
// CIR: cir.return [[RES]]
// LLVM: define {{.*}} @test_sqrtps512
// LLVM: call <16 x float> @llvm.sqrt.v16f32
// OGCG: define {{.*}} @test_sqrtps512
// OGCG: call <16 x float> @llvm.sqrt.v16f32

// Test __builtin_ia32_sqrtpd512
__m512d test_sqrtpd512(__m512d a) {
return __builtin_ia32_sqrtpd512(a);
}
// CIR: cir.func @test_sqrtpd512
// CIR: [[RES:%.*]] = cir.sqrt {{%.*}} : !cir.vector<!cir.double x 8>
// CIR: cir.return [[RES]]
// LLVM: define {{.*}} @test_sqrtpd512
// LLVM: call <8 x double> @llvm.sqrt.v8f64
// OGCG: define {{.*}} @test_sqrtpd512
// OGCG: call <8 x double> @llvm.sqrt.v8f64
Binary file added my-sqrt-changes.patch
Binary file not shown.