[clang] 56f352a - [CIR][X86] Add support for addcarryx and subborrow builtins (#239685)

via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 7 10:15:17 PST 2026


Author: Giorgis Georgakoudis
Date: 2026-11-07T10:15:11-08:00
New Revision: 56f352a7dc14a8341e5aada716a5cd415a1e287e

URL: https://github.com/llvm/llvm-project/commit/56f352a7dc14a8341e5aada716a5cd415a1e287e
DIFF: https://github.com/llvm/llvm-project/commit/56f352a7dc14a8341e5aada716a5cd415a1e287e.diff

LOG: [CIR][X86] Add support for addcarryx and subborrow builtins (#239685)

Lower __builtin_ia32_addcarryx_u32/u64 and
__builtin_ia32_subborrow_u32/u64
to the llvm.x86.addcarry.* and llvm.x86.subborrow.* intrinsics, matching
classic CodeGen.

These builtins support `_addcarryx_*` (adxintrin.h) as well as
`_addcarry_*` and
`_subborrow_*` (adcintrin.h) intrinsics, which previously were NYI.

The tests add ClangIR RUN lines to the existing
clang/test/CodeGen/X86/{adx,adc}-builtins.c: `-fclangir -emit-llvm` runs
checked
against the existing CHECK lines, and `-fclangir -emit-cir` runs with
new CIR
checks.

Assisted-by: Claude Code (Claude Opus 5.5)

Added: 
    

Modified: 
    clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
    clang/test/CodeGen/X86/adc-builtins.c
    clang/test/CodeGen/X86/adx-builtins.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
index 3f945aada16755..b46584667bac44 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp
@@ -2733,11 +2733,42 @@ CIRGenFunction::emitX86BuiltinExpr(unsigned builtinID, const CallExpr *expr) {
   case X86::BI__builtin_ia32_addcarryx_u32:
   case X86::BI__builtin_ia32_addcarryx_u64:
   case X86::BI__builtin_ia32_subborrow_u32:
-  case X86::BI__builtin_ia32_subborrow_u64:
-    cgm.errorNYI(expr->getSourceRange(),
-                 std::string("unimplemented X86 builtin call: ") +
-                     getContext().BuiltinInfo.getName(builtinID));
-    return mlir::Value{};
+  case X86::BI__builtin_ia32_subborrow_u64: {
+    llvm::StringRef intrinsicName;
+    switch (builtinID) {
+    default:
+      llvm_unreachable("Unsupported intrinsic!");
+    case X86::BI__builtin_ia32_addcarryx_u32:
+      intrinsicName = "x86.addcarry.32";
+      break;
+    case X86::BI__builtin_ia32_addcarryx_u64:
+      intrinsicName = "x86.addcarry.64";
+      break;
+    case X86::BI__builtin_ia32_subborrow_u32:
+      intrinsicName = "x86.subborrow.32";
+      break;
+    case X86::BI__builtin_ia32_subborrow_u64:
+      intrinsicName = "x86.subborrow.64";
+      break;
+    }
+
+    mlir::Location loc = getLoc(expr->getExprLoc());
+    mlir::Type carryTy = ops[0].getType();
+    mlir::Type valueTy = ops[1].getType();
+    llvm::SmallVector<mlir::Type, 2> resultTypes = {carryTy, valueTy};
+    cir::StructType resRecord =
+        builder.getAnonRecordTy(resultTypes, /*packed=*/false,
+                                cir::RecordType::getAllDataKinds(resultTypes));
+
+    mlir::Value call =
+        builder.emitIntrinsicCallOp(loc, intrinsicName, resRecord,
+                                    mlir::ValueRange{ops[0], ops[1], ops[2]});
+    mlir::Value result =
+        cir::ExtractMemberOp::create(builder, loc, valueTy, call, 1);
+    builder.CIRBaseBuilderTy::createStore(loc, result, ops[3]);
+
+    return cir::ExtractMemberOp::create(builder, loc, carryTy, call, 0);
+  }
   case X86::BI__builtin_ia32_fpclassps128_mask:
   case X86::BI__builtin_ia32_fpclassps256_mask:
   case X86::BI__builtin_ia32_fpclassps512_mask:

diff  --git a/clang/test/CodeGen/X86/adc-builtins.c b/clang/test/CodeGen/X86/adc-builtins.c
index 87e3b9a662bce1..353e5cf1ff2e69 100644
--- a/clang/test/CodeGen/X86/adc-builtins.c
+++ b/clang/test/CodeGen/X86/adc-builtins.c
@@ -1,5 +1,9 @@
 // RUN: %clang_cc1 -x c -ffreestanding -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s
 // RUN: %clang_cc1 -x c++ -ffreestanding -triple x86_64-unknown-unknown -emit-llvm -o - %s | FileCheck %s
+// RUN: %if cir-enabled %{ %clang_cc1 -x c -ffreestanding -triple x86_64-unknown-unknown -fclangir -emit-llvm -o - %s | FileCheck %s %}
+// RUN: %if cir-enabled %{ %clang_cc1 -x c++ -ffreestanding -triple x86_64-unknown-unknown -fclangir -emit-llvm -o - %s | FileCheck %s %}
+// RUN: %if cir-enabled %{ %clang_cc1 -x c -ffreestanding -triple x86_64-unknown-unknown -fclangir -emit-cir -o - %s | FileCheck %s --check-prefix=CIR %}
+// RUN: %if cir-enabled %{ %clang_cc1 -x c++ -ffreestanding -triple x86_64-unknown-unknown -fclangir -emit-cir -o - %s | FileCheck %s --check-prefix=CIR %}
 
 #include <x86intrin.h>
 
@@ -10,6 +14,13 @@ unsigned char test_addcarry_u32(unsigned char __cf, unsigned int __x,
 // CHECK: [[DATA:%.*]] = extractvalue { i8, i32 } [[ADC]], 1
 // CHECK: store i32 [[DATA]], ptr %{{.*}}
 // CHECK: [[CF:%.*]] = extractvalue { i8, i32 } [[ADC]], 0
+// CIR-LABEL: cir.func {{.*}}@{{.*}}test_addcarry_u32
+// CIR: cir.call @_addcarry_u32(
+// CIR-LABEL: cir.func {{.*}}@_addcarry_u32(
+// CIR: [[RES:%.*]] = cir.call_llvm_intrinsic "x86.addcarry.32"
+// CIR: [[VAL:%.*]] = cir.extract_member [[RES]][1] : {{.*}} -> !u32i
+// CIR: cir.store [[VAL]], {{%.*}} : !u32i, !cir.ptr<!u32i>
+// CIR: [[CF:%.*]] = cir.extract_member [[RES]][0] : {{.*}} -> !u8i
   return _addcarry_u32(__cf, __x, __y, __p);
 }
 
@@ -21,6 +32,13 @@ unsigned char test_addcarry_u64(unsigned char __cf, unsigned long long __x,
 // CHECK: [[DATA:%.*]] = extractvalue { i8, i64 } [[ADC]], 1
 // CHECK: store i64 [[DATA]], ptr %{{.*}}
 // CHECK: [[CF:%.*]] = extractvalue { i8, i64 } [[ADC]], 0
+// CIR-LABEL: cir.func {{.*}}@{{.*}}test_addcarry_u64
+// CIR: cir.call @_addcarry_u64(
+// CIR-LABEL: cir.func {{.*}}@_addcarry_u64(
+// CIR: [[RES:%.*]] = cir.call_llvm_intrinsic "x86.addcarry.64"
+// CIR: [[VAL:%.*]] = cir.extract_member [[RES]][1] : {{.*}} -> !u64i
+// CIR: cir.store [[VAL]], {{%.*}} : !u64i, !cir.ptr<!u64i>
+// CIR: [[CF:%.*]] = cir.extract_member [[RES]][0] : {{.*}} -> !u8i
   return _addcarry_u64(__cf, __x, __y, __p);
 }
 
@@ -31,6 +49,13 @@ unsigned char test_subborrow_u32(unsigned char __cf, unsigned int __x,
 // CHECK: [[DATA:%.*]] = extractvalue { i8, i32 } [[SBB]], 1
 // CHECK: store i32 [[DATA]], ptr %{{.*}}
 // CHECK: [[CF:%.*]] = extractvalue { i8, i32 } [[SBB]], 0
+// CIR-LABEL: cir.func {{.*}}@{{.*}}test_subborrow_u32
+// CIR: cir.call @_subborrow_u32(
+// CIR-LABEL: cir.func {{.*}}@_subborrow_u32(
+// CIR: [[RES:%.*]] = cir.call_llvm_intrinsic "x86.subborrow.32"
+// CIR: [[VAL:%.*]] = cir.extract_member [[RES]][1] : {{.*}} -> !u32i
+// CIR: cir.store [[VAL]], {{%.*}} : !u32i, !cir.ptr<!u32i>
+// CIR: [[CF:%.*]] = cir.extract_member [[RES]][0] : {{.*}} -> !u8i
   return _subborrow_u32(__cf, __x, __y, __p);
 }
 
@@ -42,6 +67,13 @@ unsigned char test_subborrow_u64(unsigned char __cf, unsigned long long __x,
 // CHECK: [[DATA:%.*]] = extractvalue { i8, i64 } [[SBB]], 1
 // CHECK: store i64 [[DATA]], ptr %{{.*}}
 // CHECK: [[CF:%.*]] = extractvalue { i8, i64 } [[SBB]], 0
+// CIR-LABEL: cir.func {{.*}}@{{.*}}test_subborrow_u64
+// CIR: cir.call @_subborrow_u64(
+// CIR-LABEL: cir.func {{.*}}@_subborrow_u64(
+// CIR: [[RES:%.*]] = cir.call_llvm_intrinsic "x86.subborrow.64"
+// CIR: [[VAL:%.*]] = cir.extract_member [[RES]][1] : {{.*}} -> !u64i
+// CIR: cir.store [[VAL]], {{%.*}} : !u64i, !cir.ptr<!u64i>
+// CIR: [[CF:%.*]] = cir.extract_member [[RES]][0] : {{.*}} -> !u8i
   return _subborrow_u64(__cf, __x, __y, __p);
 }
 

diff  --git a/clang/test/CodeGen/X86/adx-builtins.c b/clang/test/CodeGen/X86/adx-builtins.c
index 53861bc3ceb72d..56c454108bc758 100644
--- a/clang/test/CodeGen/X86/adx-builtins.c
+++ b/clang/test/CodeGen/X86/adx-builtins.c
@@ -1,5 +1,9 @@
 // RUN: %clang_cc1 -x c -triple x86_64-unknown-unknown -ffreestanding -target-feature +adx -emit-llvm -o - %s | FileCheck %s
 // RUN: %clang_cc1 -x c++ -triple x86_64-unknown-unknown -ffreestanding -target-feature +adx -emit-llvm -o - %s | FileCheck %s
+// RUN: %if cir-enabled %{ %clang_cc1 -x c -triple x86_64-unknown-unknown -ffreestanding -target-feature +adx -fclangir -emit-llvm -o - %s | FileCheck %s %}
+// RUN: %if cir-enabled %{ %clang_cc1 -x c++ -triple x86_64-unknown-unknown -ffreestanding -target-feature +adx -fclangir -emit-llvm -o - %s | FileCheck %s %}
+// RUN: %if cir-enabled %{ %clang_cc1 -x c -triple x86_64-unknown-unknown -ffreestanding -target-feature +adx -fclangir -emit-cir -o - %s | FileCheck %s --check-prefix=CIR %}
+// RUN: %if cir-enabled %{ %clang_cc1 -x c++ -triple x86_64-unknown-unknown -ffreestanding -target-feature +adx -fclangir -emit-cir -o - %s | FileCheck %s --check-prefix=CIR %}
 
 #include <immintrin.h>
 
@@ -10,6 +14,13 @@ unsigned char test_addcarryx_u32(unsigned char __cf, unsigned int __x,
 // CHECK: [[DATA:%.*]] = extractvalue { i8, i32 } [[ADC]], 1
 // CHECK: store i32 [[DATA]], ptr %{{.*}}
 // CHECK: [[CF:%.*]] = extractvalue { i8, i32 } [[ADC]], 0
+// CIR-LABEL: cir.func {{.*}}@{{.*}}test_addcarryx_u32
+// CIR: cir.call @_addcarryx_u32(
+// CIR-LABEL: cir.func {{.*}}@_addcarryx_u32(
+// CIR: [[RES:%.*]] = cir.call_llvm_intrinsic "x86.addcarry.32"
+// CIR: [[VAL:%.*]] = cir.extract_member [[RES]][1] : {{.*}} -> !u32i
+// CIR: cir.store [[VAL]], {{%.*}} : !u32i, !cir.ptr<!u32i>
+// CIR: [[CF:%.*]] = cir.extract_member [[RES]][0] : {{.*}} -> !u8i
   return _addcarryx_u32(__cf, __x, __y, __p);
 }
 
@@ -21,6 +32,13 @@ unsigned char test_addcarryx_u64(unsigned char __cf, unsigned long long __x,
 // CHECK: [[DATA:%.*]] = extractvalue { i8, i64 } [[ADC]], 1
 // CHECK: store i64 [[DATA]], ptr %{{.*}}
 // CHECK: [[CF:%.*]] = extractvalue { i8, i64 } [[ADC]], 0
+// CIR-LABEL: cir.func {{.*}}@{{.*}}test_addcarryx_u64
+// CIR: cir.call @_addcarryx_u64(
+// CIR-LABEL: cir.func {{.*}}@_addcarryx_u64(
+// CIR: [[RES:%.*]] = cir.call_llvm_intrinsic "x86.addcarry.64"
+// CIR: [[VAL:%.*]] = cir.extract_member [[RES]][1] : {{.*}} -> !u64i
+// CIR: cir.store [[VAL]], {{%.*}} : !u64i, !cir.ptr<!u64i>
+// CIR: [[CF:%.*]] = cir.extract_member [[RES]][0] : {{.*}} -> !u8i
   return _addcarryx_u64(__cf, __x, __y, __p);
 }
 


        


More information about the cfe-commits mailing list