[clang] [CIR] Wire SPIRV targets into the call-convention lowering pass (PR #241531)

Qi Ye via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 13 07:48:54 PST 2026


https://github.com/Qi-Ye-079 updated https://github.com/llvm/llvm-project/pull/241531

>From d8c0f6c96fdaa55a2a8f3a619c4902b120b7c64a Mon Sep 17 00:00:00 2001
From: "Ye, Qi" <qi.ye at intel.com>
Date: Wed, 11 Nov 2026 12:26:31 -0800
Subject: [PATCH 1/2] Wire SPIRV targets to CallConvLoweringPass

---
 clang/include/clang/CIR/Dialect/Passes.h      |   2 +-
 clang/include/clang/CIR/Dialect/Passes.td     |   4 +-
 .../Dialect/Transforms/CIRABIClassifier.cpp   |   6 +-
 .../CIR/Dialect/Transforms/CIRABIClassifier.h |   4 +
 .../Transforms/CIRABIClassifierSPIRV.cpp      |  75 +++++++
 .../lib/CIR/Dialect/Transforms/CMakeLists.txt |   1 +
 .../Transforms/CallConvLoweringPass.cpp       |   2 +
 .../Transforms/TargetLowering/LowerModule.cpp |   1 +
 .../abi-lowering/spirv-calling-conv.cir       | 191 ++++++++++++++++++
 .../abi-lowering/spirv-indirect.cir           | 104 ++++++++++
 .../abi-lowering/spirv-kernel-no-device.cir   |  73 +++++++
 .../abi-lowering/spirv-kernel-sycl.cir        | 103 ++++++++++
 12 files changed, 562 insertions(+), 4 deletions(-)
 create mode 100644 clang/lib/CIR/Dialect/Transforms/CIRABIClassifierSPIRV.cpp
 create mode 100644 clang/test/CIR/Transforms/abi-lowering/spirv-calling-conv.cir
 create mode 100644 clang/test/CIR/Transforms/abi-lowering/spirv-indirect.cir
 create mode 100644 clang/test/CIR/Transforms/abi-lowering/spirv-kernel-no-device.cir
 create mode 100644 clang/test/CIR/Transforms/abi-lowering/spirv-kernel-sycl.cir

diff --git a/clang/include/clang/CIR/Dialect/Passes.h b/clang/include/clang/CIR/Dialect/Passes.h
index 7c09a42bed949..126366d3a7a3e 100644
--- a/clang/include/clang/CIR/Dialect/Passes.h
+++ b/clang/include/clang/CIR/Dialect/Passes.h
@@ -20,7 +20,7 @@ namespace cir {
 /// The ABI target whose calling-convention rules drive CallConvLowering.
 /// None is the unset state used when the pass runs in classification-attr
 /// mode instead of selecting a target.
-enum class CallConvTarget { None, Test, X86_64, AMDGPU };
+enum class CallConvTarget { None, Test, X86_64, AMDGPU, SPIRV };
 } // namespace cir
 
 namespace mlir {
diff --git a/clang/include/clang/CIR/Dialect/Passes.td b/clang/include/clang/CIR/Dialect/Passes.td
index 787a9d8f29b89..9a4bf9e576e4b 100644
--- a/clang/include/clang/CIR/Dialect/Passes.td
+++ b/clang/include/clang/CIR/Dialect/Passes.td
@@ -245,6 +245,7 @@ def CallConvLowering : Pass<"cir-call-conv-lowering", "mlir::ModuleOp"> {
       target from the module triple.  Supported targets:
       - `"x86_64"`: System V.
       - `"amdgpu"`: AMDGPU.
+      - `"spirv"`: SPIR-V (`spirv`, `spirv32`, `spirv64` triples).
       - `"test"`: the MLIR test target in `mlir/lib/ABI/Targets/Test/`.
       Other targets will be added as the LLVM ABI library ships them.
     - `classification-attr=<name>` reads a `DictionaryAttr` named `<name>`
@@ -266,7 +267,8 @@ def CallConvLowering : Pass<"cir-call-conv-lowering", "mlir::ModuleOp"> {
              clEnumValN(cir::CallConvTarget::X86_64, "x86_64",
                         "x86_64 System V"),
              clEnumValN(cir::CallConvTarget::AMDGPU, "amdgpu",
-                        "AMDGPU")
+                        "AMDGPU"),
+             clEnumValN(cir::CallConvTarget::SPIRV, "spirv", "SPIR-V")
            )}]>,
     Option<"classificationAttr", "classification-attr", "std::string",
            /*default=*/"\"\"",
diff --git a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp
index 21d5af11f31fc..fd3982cb30402 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp
@@ -780,11 +780,13 @@ CIRABIClassifierCommon::classifySignature(
   return fc;
 }
 
-/// FIXME: Only the AMDGPU kernel convention is mapped. Every other convention
-/// is classified as C.
+/// FIXME: Only the AMDGPU and SPIR kernel conventions are mapped. Every other
+/// convention is classified as C.
 llvm::CallingConv::ID cir::convertCallingConv(cir::CallingConv cc) {
   if (cc == cir::CallingConv::AMDGPUKernel)
     return llvm::CallingConv::AMDGPU_KERNEL;
+  if (cc == cir::CallingConv::SpirKernel)
+    return llvm::CallingConv::SPIR_KERNEL;
   return llvm::CallingConv::C;
 }
 
diff --git a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.h b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.h
index edbdc40c4d2e0..1250465402986 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.h
+++ b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.h
@@ -172,6 +172,10 @@ createX86_64ABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp,
 std::unique_ptr<CIRABIClassifier>
 createAMDGPUABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp);
 
+/// The SPIR-V classifier.
+std::unique_ptr<CIRABIClassifier>
+createSPIRVABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp);
+
 /// Map a CIR calling convention to the llvm::CallingConv the ABI classifier
 /// uses.  Conventions the classifier does not distinguish fall back to C.
 llvm::CallingConv::ID convertCallingConv(cir::CallingConv cc);
diff --git a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierSPIRV.cpp b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierSPIRV.cpp
new file mode 100644
index 0000000000000..cc6d6205445d6
--- /dev/null
+++ b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierSPIRV.cpp
@@ -0,0 +1,75 @@
+//===- CIRABIClassifierSPIRV.cpp - SPIR-V classification ------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the SPIR-V classifier, which classifies with the LLVM
+// ABI Lowering Library's SPIR-V target.
+//
+//===----------------------------------------------------------------------===//
+
+#include "CIRABIClassifier.h"
+#include "TargetLowering/LowerModule.h"
+
+#include "clang/CIR/Dialect/IR/CIRDialect.h"
+
+using namespace mlir;
+using namespace mlir::abi;
+using namespace cir;
+
+namespace {
+
+class SPIRVABIClassifier final : public CIRABIClassifierCommon {
+public:
+  SPIRVABIClassifier(const mlir::DataLayout &dl, mlir::ModuleOp modOp)
+      : CIRABIClassifierCommon(dl, modOp) {}
+
+  mlir::FailureOr<ArgClassification>
+  classifyVAArg(cir::VAArgOp vaArg) override {
+    vaArg->emitOpError()
+        << "not yet implemented in CallConvLowering for SPIR-V";
+    return mlir::failure();
+  }
+
+protected:
+  /// SPIR-V has no per-function feature that changes its ABI rules.
+  const llvm::abi::TargetInfo &getTargetInfo(cir::FuncOp) override;
+
+private:
+  std::unique_ptr<llvm::abi::TargetInfo> target;
+};
+
+const llvm::abi::TargetInfo &SPIRVABIClassifier::getTargetInfo(cir::FuncOp) {
+  if (!target) {
+    // Classic codegen passes the target address space of LangAS::Default and
+    // LangOptions::isTargetDevice().
+    // LowerModule rebuilds both from the module's attributes, so a reloaded
+    // .cir classifies the same way it was compiled. Without cir.triple
+    // there is no target, so use 0 as default address space.
+    std::unique_ptr<cir::LowerModule> lowerModule =
+        cir::createLowerModule(modOp);
+    assert(lowerModule && "LowerModule shouldn't be null");
+    unsigned defaultAddrSpace =
+        lowerModule->hasTarget()
+            ? lowerModule->getTarget().getTargetAddressSpace(
+                  clang::LangAS::Default)
+            : 0;
+
+    target = llvm::abi::createSPIRVTargetInfo(
+        typeMapper.getTypeBuilder(), defaultAddrSpace,
+        /*KernelPassAggregatesIndirect=*/
+        lowerModule->getLangOpts().isTargetDevice());
+  }
+  return *target;
+}
+
+} // namespace
+
+std::unique_ptr<CIRABIClassifier>
+cir::createSPIRVABIClassifier(const mlir::DataLayout &dl,
+                              mlir::ModuleOp modOp) {
+  return std::make_unique<SPIRVABIClassifier>(dl, modOp);
+}
diff --git a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
index c379da7d64794..343b9b1b4e3b7 100644
--- a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
+++ b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
@@ -18,6 +18,7 @@ add_clang_library(MLIRCIRTransforms
   RecordTypeConverter.cpp
   CIRABIClassifier.cpp
   CIRABIClassifierAMDGPU.cpp
+  CIRABIClassifierSPIRV.cpp
   CIRABIClassifierX86_64.cpp
 
   DEPENDS
diff --git a/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp b/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp
index 846bc410626fb..c101cb6d26fd2 100644
--- a/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp
@@ -148,6 +148,8 @@ createABIClassifier(cir::CallConvTarget target,
     return createX86_64ABIClassifier(dl, modOp, options);
   case cir::CallConvTarget::AMDGPU:
     return createAMDGPUABIClassifier(dl, modOp);
+  case cir::CallConvTarget::SPIRV:
+    return createSPIRVABIClassifier(dl, modOp);
   case cir::CallConvTarget::None:
     break;
   }
diff --git a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerModule.cpp b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerModule.cpp
index 20a0a4e2e1e9a..275bb8c3a3772 100644
--- a/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerModule.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerModule.cpp
@@ -113,6 +113,7 @@ std::unique_ptr<LowerModule> createLowerModule(mlir::ModuleOp module) {
     langOpts.GPURelocatableDeviceCode = loweringLangOpts.getGpuRdc();
     langOpts.OpenMP = loweringLangOpts.getOpenmp();
     langOpts.OpenMPIsTargetDevice = loweringLangOpts.getOpenmpIsTargetDevice();
+    langOpts.SYCLIsDevice = loweringLangOpts.getSyclIsDevice();
     langOpts.setClangABICompat(static_cast<clang::LangOptions::ClangABI>(
         loweringLangOpts.getClangAbiCompat()));
     langOpts.LongDoubleSize = loweringLangOpts.getLongDoubleSize();
diff --git a/clang/test/CIR/Transforms/abi-lowering/spirv-calling-conv.cir b/clang/test/CIR/Transforms/abi-lowering/spirv-calling-conv.cir
new file mode 100644
index 0000000000000..2a8bc04f8f924
--- /dev/null
+++ b/clang/test/CIR/Transforms/abi-lowering/spirv-calling-conv.cir
@@ -0,0 +1,191 @@
+// RUN: cir-opt %s -split-input-file -cir-call-conv-lowering=target=spirv | FileCheck %s
+
+// Checks that CallConvLowering under target=spirv classifies ordinary SPIR-V
+// functions with the LLVM ABI library's default rules, as classic codegen's
+// SPIRVABIInfo does: sub-word integers are extended, aggregates are passed
+// byval and returned through sret, and empty records are still passed byval.
+// SPIR-V has no 128-bit integer type, so a _BitInt wider than 64 bits is passed
+// and returned indirectly.
+
+!s8i = !cir.int<s, 8>
+!s32i = !cir.int<s, 32>
+!u8i = !cir.int<u, 8>
+!bi64 = !cir.int<s, 64, bitint>
+!bi65 = !cir.int<s, 65, bitint>
+!rec_S = !cir.struct<"S" {data !s32i, data !cir.float}>
+!rec_E = !cir.struct<"E" {pad !u8i}>
+
+// The module has no #cir.lowering_options, as in hand-written CIR, so no
+// device compilation is assumed.
+module attributes {
+  cir.triple = "spirv64-unknown-unknown",
+  cir.record_layouts = {
+    S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+      has_trivial_dtor = true, record_align = 4>,
+    E = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+      has_trivial_dtor = true, record_align = 1>},
+  dlti.dl_spec = #dlti.dl_spec<
+    #dlti.dl_entry<i1, dense<8>: vector<2xi64>>,
+    #dlti.dl_entry<i8, dense<8>: vector<2xi64>>,
+    #dlti.dl_entry<i16, dense<16>: vector<2xi64>>,
+    #dlti.dl_entry<i32, dense<32>: vector<2xi64>>,
+    #dlti.dl_entry<i64, dense<64>: vector<2xi64>>>
+} {
+
+  // A signed sub-word integer is sign-extended.
+  cir.func @take_s8(%arg0: !s8i) cc(spir_function) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_s8(%arg0: !s8i {
+  // CHECK-SAME: llvm.signext
+
+  // The call site carries the same extension.
+  cir.func @call_s8(%arg0: !s8i) cc(spir_function) requires_call_conv_lowering {
+    cir.call @take_s8(%arg0) : (!s8i) -> ()
+    cir.return
+  }
+
+  // CHECK: cir.call @take_s8(%arg0) : (!s8i {
+  // CHECK-SAME: llvm.signext}) -> ()
+
+  // A bool return is zero-extended.
+  cir.func @ret_bool(%arg0: !cir.bool) -> !cir.bool cc(spir_function) requires_call_conv_lowering {
+    cir.return %arg0 : !cir.bool
+  }
+
+  // CHECK: cir.func{{.*}} @ret_bool({{.*}}) -> (!cir.bool {
+  // CHECK-SAME: llvm.zeroext
+
+  // An unsigned sub-word integer is zero-extended.
+  cir.func @take_u8(%arg0: !u8i) cc(spir_function) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_u8(%arg0: !u8i {
+  // CHECK-SAME: llvm.zeroext
+
+  // A signed sub-word return is sign-extended, at the call site too.
+  cir.func @ret_s8(%arg0: !s8i) -> !s8i cc(spir_function) requires_call_conv_lowering {
+    cir.return %arg0 : !s8i
+  }
+
+  // CHECK: cir.func{{.*}} @ret_s8({{.*}}) -> (!s8i {
+  // CHECK-SAME: llvm.signext
+
+  cir.func @call_ret_s8(%arg0: !s8i) cc(spir_function) requires_call_conv_lowering {
+    %0 = cir.call @ret_s8(%arg0) cc(spir_function) : (!s8i) -> !s8i
+    cir.return
+  }
+
+  // CHECK: cir.call @ret_s8(%arg0) cc(spir_function) : (!s8i {
+  // CHECK-SAME: -> (!s8i {llvm.signext})
+
+  // A _BitInt of up to 64 bits is passed and returned directly.
+  cir.func @take_bi64(%arg0: !bi64) cc(spir_function) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_bi64(%arg0: !s64i_bitint) cc(spir_function)
+
+  cir.func @ret_bi64(%arg0: !bi64) -> !bi64 cc(spir_function) requires_call_conv_lowering {
+    cir.return %arg0 : !bi64
+  }
+
+  // CHECK: cir.func{{.*}} @ret_bi64(%arg0: !s64i_bitint) -> !s64i_bitint cc(spir_function)
+
+  // A wider _BitInt is passed byval and returned through sret.
+  cir.func @take_bi65(%arg0: !bi65) cc(spir_function) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_bi65(%{{.*}}: !cir.ptr<!cir.int<s, 65, bitint>> {
+  // CHECK-SAME: llvm.align = 8 : i64
+  // CHECK-SAME: llvm.byval = !cir.int<s, 65, bitint>
+
+  cir.func @ret_bi65(%arg0: !cir.ptr<!bi65>) -> !bi65 cc(spir_function) requires_call_conv_lowering {
+    %0 = cir.alloca "__retval" align(8) : !cir.ptr<!bi65>
+    %1 = cir.load %arg0 : !cir.ptr<!bi65>, !bi65
+    cir.store %1, %0 : !bi65, !cir.ptr<!bi65>
+    %2 = cir.load %0 : !cir.ptr<!bi65>, !bi65
+    cir.return %2 : !bi65
+  }
+
+  // CHECK: cir.func{{.*}} @ret_bi65(%{{.*}}: !cir.ptr<!cir.int<s, 65, bitint>> {
+  // CHECK-SAME: llvm.align = 8 : i64
+  // CHECK-SAME: llvm.sret = !cir.int<s, 65, bitint>
+
+  // An aggregate argument is passed byval in the private address space.
+  cir.func @take_rec(%arg0: !rec_S) cc(spir_function) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_rec(%{{.*}}: !cir.ptr<!rec_S> {
+  // CHECK-SAME: llvm.align = 4 : i64
+  // CHECK-SAME: llvm.byval = !rec_S
+
+  // An empty record is still an aggregate: passed byval, not dropped.
+  cir.func @take_empty(%arg0: !rec_E, %arg1: !s32i) cc(spir_function) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_empty(%{{.*}}: !cir.ptr<!rec_E> {
+  // CHECK-SAME: llvm.byval = !rec_E
+  // CHECK-SAME: !s32i
+
+  // An aggregate return goes through an sret pointer, so the function returns
+  // nothing.
+  cir.func @ret_rec() -> !rec_S cc(spir_function) requires_call_conv_lowering {
+    %0 = cir.alloca "s" align(4) : !cir.ptr<!rec_S>
+    %1 = cir.load %0 : !cir.ptr<!rec_S>, !rec_S
+    cir.return %1 : !rec_S
+  }
+
+  // CHECK: cir.func{{.*}} @ret_rec(%{{.*}}: !cir.ptr<!rec_S> {
+  // CHECK-SAME: llvm.align = 4 : i64
+  // CHECK-SAME: llvm.sret = !rec_S
+  // CHECK-SAME: }) cc(spir_function)
+
+  // Pointers and word-sized scalars of ordinary functions are unchanged.
+  cir.func @take_ptr(%arg0: !cir.ptr<!s32i>) cc(spir_function) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_ptr(%arg0: !cir.ptr<!s32i>
+
+  cir.func @take_int(%arg0: !s32i) cc(spir_function) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @take_int(%arg0: !s32i
+
+  // With no lowering_options the default address space is 0, so a private
+  // kernel pointer is the default one and is passed as a CrossWorkgroup
+  // pointer.
+  cir.func @kernel_ptr(%arg0: !cir.ptr<!s32i>) cc(spir_kernel) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @kernel_ptr(%arg0: !cir.ptr<!s32i, target_address_space(1)>
+}
+
+// -----
+
+// Without a cir.triple the target cannot be rebuilt, so the default address
+// space falls back to 0.
+
+!s32i = !cir.int<s, 32>
+
+module attributes {
+  dlti.dl_spec = #dlti.dl_spec<
+    #dlti.dl_entry<i32, dense<32>: vector<2xi64>>,
+    #dlti.dl_entry<i64, dense<64>: vector<2xi64>>>
+} {
+
+  // The default address space is 0, so a private kernel pointer is coerced.
+  cir.func @kernel_ptr_no_triple(%arg0: !cir.ptr<!s32i>) cc(spir_kernel) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK: cir.func{{.*}} @kernel_ptr_no_triple(%arg0: !cir.ptr<!s32i, target_address_space(1)>
+}
diff --git a/clang/test/CIR/Transforms/abi-lowering/spirv-indirect.cir b/clang/test/CIR/Transforms/abi-lowering/spirv-indirect.cir
new file mode 100644
index 0000000000000..434879e3d2524
--- /dev/null
+++ b/clang/test/CIR/Transforms/abi-lowering/spirv-indirect.cir
@@ -0,0 +1,104 @@
+// RUN: cir-opt %s -cir-call-conv-lowering=target=spirv | FileCheck %s
+
+// SPIR-V records that cannot be copied, in the shape SYCL CIRGen emits: every
+// local is an address space 0 alloca used through a generic (4) view. As in
+// classic codegen:
+// - one is returned through an sret pointer in the default address space,
+//   which is generic for SYCL;
+// - one is passed to a device function as a pointer to the caller's
+//   copy-constructed temporary, not byval, and the callee works on it in place.
+
+!s32i = !cir.int<s, 32>
+!rec_NT = !cir.struct<"NT" {data !s32i}>
+
+module attributes {
+  cir.triple = "spirv64-unknown-unknown",
+  cir.lowering_options = #cir.lowering_options<
+    exceptions = false, threadsafe_statics = true, cuda = false,
+    cuda_is_device = false, hip = false, hlsl = false, sycl_is_device = true,
+    gpu_rdc = false, openmp = false, openmp_is_target_device = false,
+    clang_abi_compat = 17, long_double_size = 0, enable_noundef_attrs = true,
+    no_trapping_math = true, ssp_buffer_size = 8>,
+  cir.record_layouts = {NT = #cir.record_layout<
+    arg_passing_kind = cannot_pass_in_regs, has_trivial_dtor = false,
+    record_align = 4>},
+  dlti.dl_spec = #dlti.dl_spec<
+    #dlti.dl_entry<i32, dense<32>: vector<2xi64>>,
+    #dlti.dl_entry<i64, dense<64>: vector<2xi64>>>
+} {
+
+  cir.func private @init_nt(!cir.ptr<!rec_NT, target_address_space(4)>) cc(spir_function)
+  cir.func private @copy_nt(!cir.ptr<!rec_NT, target_address_space(4)>, !cir.ptr<!rec_NT, target_address_space(4)>) cc(spir_function)
+  cir.func private @use_nt(!cir.ptr<!rec_NT, target_address_space(4)>) cc(spir_function)
+  cir.func private @make_nt() -> !rec_NT cc(spir_function) requires_call_conv_lowering
+
+  // CHECK: cir.func private @make_nt(!cir.ptr<!rec_NT, target_address_space(4)> {llvm.align = 4 : i64, llvm.dead_on_unwind, llvm.sret = !rec_NT, llvm.writable}) cc(spir_function)
+
+  // The callee constructs its result directly in the caller's generic sret
+  // memory.
+  cir.func @ret_nt() -> !rec_NT cc(spir_function) requires_call_conv_lowering {
+    %0 = cir.alloca "__retval" align(4) : !cir.ptr<!rec_NT>
+    %1 = cir.cast address_space %0 : !cir.ptr<!rec_NT> -> !cir.ptr<!rec_NT, target_address_space(4)>
+    cir.call @init_nt(%1) cc(spir_function) : (!cir.ptr<!rec_NT, target_address_space(4)>) -> ()
+    %2 = cir.load %0 : !cir.ptr<!rec_NT>, !rec_NT
+    cir.return %2 : !rec_NT
+  }
+
+  // CHECK:      cir.func @ret_nt(%[[SRET:.*]]: !cir.ptr<!rec_NT, target_address_space(4)> {llvm.align = 4 : i64, llvm.dead_on_unwind, llvm.noalias, llvm.sret = !rec_NT, llvm.writable}) cc(spir_function) {
+  // CHECK-NEXT:   cir.call @init_nt(%[[SRET]]) cc(spir_function) : (!cir.ptr<!rec_NT, target_address_space(4)>) -> ()
+  // CHECK-NEXT:   cir.return
+  // CHECK-NEXT: }
+
+  // The caller passes the generic view of its local as the sret pointer, so
+  // the callee constructs straight into `x`.
+  cir.func @use_ret() cc(spir_function) requires_call_conv_lowering {
+    %0 = cir.alloca "x" align(4) init : !cir.ptr<!rec_NT>
+    %1 = cir.cast address_space %0 : !cir.ptr<!rec_NT> -> !cir.ptr<!rec_NT, target_address_space(4)>
+    %2 = cir.call @make_nt() cc(spir_function) : () -> !rec_NT
+    cir.store align(4) %2, %1 : !rec_NT, !cir.ptr<!rec_NT, target_address_space(4)>
+    cir.call @use_nt(%1) cc(spir_function) : (!cir.ptr<!rec_NT, target_address_space(4)>) -> ()
+    cir.return
+  }
+
+  // CHECK:      cir.func @use_ret() cc(spir_function) {
+  // CHECK-NEXT:   %[[X:.*]] = cir.alloca "x" align(4) init : !cir.ptr<!rec_NT>
+  // CHECK-NEXT:   %[[X_VIEW:.*]] = cir.cast address_space %[[X]] : !cir.ptr<!rec_NT> -> !cir.ptr<!rec_NT, target_address_space(4)>
+  // CHECK-NEXT:   cir.call @make_nt(%[[X_VIEW]]) cc(spir_function) : (!cir.ptr<!rec_NT, target_address_space(4)> {llvm.align = 4 : i64, llvm.dead_on_unwind, llvm.sret = !rec_NT, llvm.writable}) -> ()
+  // CHECK-NEXT:   cir.call @use_nt(%[[X_VIEW]]) cc(spir_function) : (!cir.ptr<!rec_NT, target_address_space(4)>) -> ()
+  // CHECK-NEXT:   cir.return
+  // CHECK-NEXT: }
+
+  // The callee works on the caller's object in place: the incoming private
+  // pointer replaces the parameter's alloca, under the same generic view.
+  cir.func @take_nt(%arg0: !rec_NT) cc(spir_function) requires_call_conv_lowering {
+    %0 = cir.alloca "n" align(4) init : !cir.ptr<!rec_NT>
+    %1 = cir.cast address_space %0 : !cir.ptr<!rec_NT> -> !cir.ptr<!rec_NT, target_address_space(4)>
+    cir.store %arg0, %1 : !rec_NT, !cir.ptr<!rec_NT, target_address_space(4)>
+    cir.call @use_nt(%1) cc(spir_function) : (!cir.ptr<!rec_NT, target_address_space(4)>) -> ()
+    cir.return
+  }
+
+  // CHECK:      cir.func @take_nt(%[[N:.*]]: !cir.ptr<!rec_NT> {llvm.align = 4 : i64, llvm.dereferenceable = 4 : i64, llvm.nofreeobj, llvm.noundef}) cc(spir_function) {
+  // CHECK-NEXT:   %[[N_VIEW:.*]] = cir.cast address_space %[[N]] : !cir.ptr<!rec_NT> -> !cir.ptr<!rec_NT, target_address_space(4)>
+  // CHECK-NEXT:   cir.call @use_nt(%[[N_VIEW]]) cc(spir_function) : (!cir.ptr<!rec_NT, target_address_space(4)>) -> ()
+  // CHECK-NEXT:   cir.return
+  // CHECK-NEXT: }
+
+  // The caller passes the copy-constructed temporary itself, not a byte copy.
+  cir.func @call_nt(%arg0: !cir.ptr<!rec_NT, target_address_space(4)>) cc(spir_function) requires_call_conv_lowering {
+    %0 = cir.alloca "agg.tmp0" align(4) : !cir.ptr<!rec_NT>
+    %1 = cir.cast address_space %0 : !cir.ptr<!rec_NT> -> !cir.ptr<!rec_NT, target_address_space(4)>
+    cir.call @copy_nt(%1, %arg0) cc(spir_function) : (!cir.ptr<!rec_NT, target_address_space(4)>, !cir.ptr<!rec_NT, target_address_space(4)>) -> ()
+    %2 = cir.load %1 : !cir.ptr<!rec_NT, target_address_space(4)>, !rec_NT
+    cir.call @take_nt(%2) cc(spir_function) : (!rec_NT) -> ()
+    cir.return
+  }
+
+  // CHECK:      cir.func @call_nt(%[[SRC:.*]]: !cir.ptr<!rec_NT, target_address_space(4)>) cc(spir_function) {
+  // CHECK-NEXT:   %[[TMP:.*]] = cir.alloca "agg.tmp0" align(4) : !cir.ptr<!rec_NT>
+  // CHECK-NEXT:   %[[TMP_VIEW:.*]] = cir.cast address_space %[[TMP]] : !cir.ptr<!rec_NT> -> !cir.ptr<!rec_NT, target_address_space(4)>
+  // CHECK-NEXT:   cir.call @copy_nt(%[[TMP_VIEW]], %[[SRC]]) cc(spir_function) : (!cir.ptr<!rec_NT, target_address_space(4)>, !cir.ptr<!rec_NT, target_address_space(4)>) -> ()
+  // CHECK-NEXT:   cir.call @take_nt(%[[TMP]]) cc(spir_function) : (!cir.ptr<!rec_NT> {llvm.align = 4 : i64, llvm.dereferenceable = 4 : i64, llvm.nofreeobj, llvm.noundef}) -> ()
+  // CHECK-NEXT:   cir.return
+  // CHECK-NEXT: }
+}
diff --git a/clang/test/CIR/Transforms/abi-lowering/spirv-kernel-no-device.cir b/clang/test/CIR/Transforms/abi-lowering/spirv-kernel-no-device.cir
new file mode 100644
index 0000000000000..49615aa3a15f6
--- /dev/null
+++ b/clang/test/CIR/Transforms/abi-lowering/spirv-kernel-no-device.cir
@@ -0,0 +1,73 @@
+// RUN: cir-opt %s -cir-call-conv-lowering=target=spirv | FileCheck %s
+
+// Checks SPIR-V kernel argument classification outside a device compilation.
+// The default address space is then 0, and kernel pointer coercion follows the
+// default address space rather than the device flag, as in classic codegen:
+// - a default (address space 0) pointer is still passed as a CrossWorkgroup
+//   (1) pointer, and a generic (4) one is left alone;
+// - without the device flag, a record that cannot be copied is passed
+//   indirectly but not byval. Copyable aggregates are byval either way.
+
+!s32i = !cir.int<s, 32>
+!rec_S = !cir.struct<"S" {data !s32i, data !cir.float}>
+!rec_NT = !cir.struct<"NT" {data !s32i}>
+
+module attributes {
+  cir.triple = "spirv64-unknown-unknown",
+  cir.lowering_options = #cir.lowering_options<
+    exceptions = false, threadsafe_statics = true, cuda = false,
+    cuda_is_device = false, hip = false, hlsl = false, sycl_is_device = false,
+    gpu_rdc = false, openmp = false, openmp_is_target_device = false,
+    clang_abi_compat = 17, long_double_size = 0, enable_noundef_attrs = true,
+    no_trapping_math = true, ssp_buffer_size = 8>,
+  cir.record_layouts = {
+    S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+      has_trivial_dtor = true, record_align = 4>,
+    NT = #cir.record_layout<arg_passing_kind = cannot_pass_in_regs,
+      has_trivial_dtor = false, record_align = 4>},
+  dlti.dl_spec = #dlti.dl_spec<
+    #dlti.dl_entry<i32, dense<32>: vector<2xi64>>,
+    #dlti.dl_entry<i64, dense<64>: vector<2xi64>>>
+} {
+
+  // A default address space pointer is passed as a CrossWorkgroup pointer and
+  // cast back before the body's spill reads it.
+  cir.func @kernel_ptr(%arg0: !cir.ptr<!s32i>) cc(spir_kernel) requires_call_conv_lowering {
+    %0 = cir.alloca "p" align(8) init : !cir.ptr<!cir.ptr<!s32i>>
+    cir.store %arg0, %0 : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @kernel_ptr(%[[ARG:[^:]+]]: !cir.ptr<!s32i, target_address_space(1)>{{.*}}cc(spir_kernel)
+  // CHECK-NEXT:   %[[DEF:.*]] = cir.cast address_space %[[ARG]] : !cir.ptr<!s32i, target_address_space(1)> -> !cir.ptr<!s32i>
+  // CHECK:        cir.store %[[DEF]], %{{.*}} : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
+
+  // A generic pointer is not the default one here, so it is left alone.
+  cir.func @kernel_gen_ptr(%arg0: !cir.ptr<!s32i, target_address_space(4)>) cc(spir_kernel) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @kernel_gen_ptr(%arg0: !cir.ptr<!s32i, target_address_space(4)>{{.*}}cc(spir_kernel)
+  // CHECK-NOT:    cir.cast
+
+  // A copyable aggregate is byval through the default rule.
+  cir.func @kernel_rec(%arg0: !rec_S) cc(spir_kernel) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @kernel_rec(%{{.*}}: !cir.ptr<!rec_S> {
+  // CHECK-SAME:   llvm.byval = !rec_S
+
+  // A record that cannot be copied is passed indirectly, not byval.
+  cir.func @kernel_nt(%arg0: !rec_NT) cc(spir_kernel) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @kernel_nt(%{{.*}}: !cir.ptr<!rec_NT>
+  // CHECK-NOT:    llvm.byval
+  // CHECK:      cir.func{{.*}} @sentinel
+
+  cir.func @sentinel() cc(spir_function) requires_call_conv_lowering {
+    cir.return
+  }
+}
diff --git a/clang/test/CIR/Transforms/abi-lowering/spirv-kernel-sycl.cir b/clang/test/CIR/Transforms/abi-lowering/spirv-kernel-sycl.cir
new file mode 100644
index 0000000000000..a6c2b56a952ed
--- /dev/null
+++ b/clang/test/CIR/Transforms/abi-lowering/spirv-kernel-sycl.cir
@@ -0,0 +1,103 @@
+// RUN: cir-opt %s -cir-call-conv-lowering=target=spirv | FileCheck %s
+
+// Checks SPIR-V kernel argument classification for SYCL device code. The
+// SYCL flag in the module's lowering_options makes the generic address
+// space (4) the default one and marks the compilation as a device one, so:
+// - a default address space pointer is passed as a CrossWorkgroup (1)
+//   pointer and cast back to generic in the kernel;
+// - every aggregate, including one that cannot be copied, is passed byval.
+
+!s8i = !cir.int<s, 8>
+!s32i = !cir.int<s, 32>
+!rec_S = !cir.struct<"S" {data !s32i, data !cir.float}>
+!rec_NT = !cir.struct<"NT" {data !s32i}>
+
+module attributes {
+  cir.triple = "spirv64-unknown-unknown",
+  cir.lowering_options = #cir.lowering_options<
+    exceptions = false, threadsafe_statics = true, cuda = false,
+    cuda_is_device = false, hip = false, hlsl = false, sycl_is_device = true,
+    gpu_rdc = false, openmp = false, openmp_is_target_device = false,
+    clang_abi_compat = 17, long_double_size = 0, enable_noundef_attrs = true,
+    no_trapping_math = true, ssp_buffer_size = 8>,
+  cir.record_layouts = {
+    S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+      has_trivial_dtor = true, record_align = 4>,
+    NT = #cir.record_layout<arg_passing_kind = cannot_pass_in_regs,
+      has_trivial_dtor = false, record_align = 4>},
+  dlti.dl_spec = #dlti.dl_spec<
+    #dlti.dl_entry<i1, dense<8>: vector<2xi64>>,
+    #dlti.dl_entry<i8, dense<8>: vector<2xi64>>,
+    #dlti.dl_entry<i16, dense<16>: vector<2xi64>>,
+    #dlti.dl_entry<i32, dense<32>: vector<2xi64>>,
+    #dlti.dl_entry<i64, dense<64>: vector<2xi64>>>
+} {
+
+  // A generic pointer is passed as a CrossWorkgroup pointer and cast back to
+  // generic before the body's spill reads it.
+  cir.func @kernel_ptr(%arg0: !cir.ptr<!s32i, target_address_space(4)>) cc(spir_kernel) requires_call_conv_lowering {
+    %0 = cir.alloca "p" align(8) init : !cir.ptr<!cir.ptr<!s32i, target_address_space(4)>>
+    cir.store %arg0, %0 : !cir.ptr<!s32i, target_address_space(4)>, !cir.ptr<!cir.ptr<!s32i, target_address_space(4)>>
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @kernel_ptr(%[[ARG:[^:]+]]: !cir.ptr<!s32i, target_address_space(1)>{{.*}}cc(spir_kernel)
+  // CHECK-NEXT:   %[[GEN:.*]] = cir.cast address_space %[[ARG]] : !cir.ptr<!s32i, target_address_space(1)> -> !cir.ptr<!s32i, target_address_space(4)>
+  // CHECK:        cir.store %[[GEN]], %{{.*}} : !cir.ptr<!s32i, target_address_space(4)>
+
+  // A pointer already in CrossWorkgroup is left alone.
+  cir.func @kernel_global_ptr(%arg0: !cir.ptr<!s32i, target_address_space(1)>) cc(spir_kernel) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @kernel_global_ptr(%arg0: !cir.ptr<!s32i, target_address_space(1)>{{.*}}cc(spir_kernel)
+  // CHECK-NOT:    cir.cast
+
+  // A private (address space 0) pointer is not the default one here, so it
+  // is not coerced either.
+  cir.func @kernel_private_ptr(%arg0: !cir.ptr<!s32i>) cc(spir_kernel) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @kernel_private_ptr(%arg0: !cir.ptr<!s32i>{{.*}}cc(spir_kernel)
+  // CHECK-NOT:    cir.cast
+
+  // An aggregate is passed byval in the private address space.
+  cir.func @kernel_rec(%arg0: !rec_S) cc(spir_kernel) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @kernel_rec(%{{.*}}: !cir.ptr<!rec_S> {
+  // CHECK-SAME:   llvm.align = 4 : i64
+  // CHECK-SAME:   llvm.byval = !rec_S
+
+  // A record that cannot be copied is still passed byval in device code.
+  cir.func @kernel_nt(%arg0: !rec_NT) cc(spir_kernel) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @kernel_nt(%{{.*}}: !cir.ptr<!rec_NT> {
+  // CHECK-SAME:   llvm.byval = !rec_NT
+
+  // Unlike AMDGPU, SPIR-V extends sub-word kernel arguments.
+  cir.func @kernel_s8(%arg0: !s8i) cc(spir_kernel) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @kernel_s8(%arg0: !s8i {
+  // CHECK-SAME:   llvm.signext
+
+  cir.func @kernel_bool(%arg0: !cir.bool) cc(spir_kernel) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @kernel_bool(%arg0: !cir.bool {
+  // CHECK-SAME:   llvm.zeroext
+
+  // Only kernels are coerced: a device function keeps the generic pointer.
+  cir.func @device_ptr(%arg0: !cir.ptr<!s32i, target_address_space(4)>) cc(spir_function) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @device_ptr(%arg0: !cir.ptr<!s32i, target_address_space(4)>{{.*}}cc(spir_function)
+}

>From d530cb07a9b4dc19c6d76eae0951666a61eff6d9 Mon Sep 17 00:00:00 2001
From: "Ye, Qi" <qi.ye at intel.com>
Date: Thu, 12 Nov 2026 13:23:57 -0800
Subject: [PATCH 2/2] Fix comments

- Removed some unnecessary comments
- Removed the testcase with a cir.triple
- Added a sycl-intel.cir tests to cover spirv64-intel (without
  sycl_is_device flag since the Intel triple alone sets default AS as 4)
- Improved some comments in tests
---
 .../Dialect/Transforms/CIRABIClassifier.cpp   |  3 +-
 .../Transforms/CIRABIClassifierSPIRV.cpp      |  6 +-
 .../abi-lowering/spirv-calling-conv.cir       | 21 -----
 .../Transforms/abi-lowering/spirv-intel.cir   | 85 +++++++++++++++++++
 .../abi-lowering/spirv-kernel-no-device.cir   |  5 +-
 5 files changed, 90 insertions(+), 30 deletions(-)
 create mode 100644 clang/test/CIR/Transforms/abi-lowering/spirv-intel.cir

diff --git a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp
index fd3982cb30402..d1d9c0ce59644 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp
@@ -780,8 +780,7 @@ CIRABIClassifierCommon::classifySignature(
   return fc;
 }
 
-/// FIXME: Only the AMDGPU and SPIR kernel conventions are mapped. Every other
-/// convention is classified as C.
+/// FIXME: Implement proper support for other calling conventions.
 llvm::CallingConv::ID cir::convertCallingConv(cir::CallingConv cc) {
   if (cc == cir::CallingConv::AMDGPUKernel)
     return llvm::CallingConv::AMDGPU_KERNEL;
diff --git a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierSPIRV.cpp b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierSPIRV.cpp
index cc6d6205445d6..4de27e4eb15bb 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierSPIRV.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifierSPIRV.cpp
@@ -44,11 +44,7 @@ class SPIRVABIClassifier final : public CIRABIClassifierCommon {
 
 const llvm::abi::TargetInfo &SPIRVABIClassifier::getTargetInfo(cir::FuncOp) {
   if (!target) {
-    // Classic codegen passes the target address space of LangAS::Default and
-    // LangOptions::isTargetDevice().
-    // LowerModule rebuilds both from the module's attributes, so a reloaded
-    // .cir classifies the same way it was compiled. Without cir.triple
-    // there is no target, so use 0 as default address space.
+    // Without cir.triple there is no target, so use 0 as default address space.
     std::unique_ptr<cir::LowerModule> lowerModule =
         cir::createLowerModule(modOp);
     assert(lowerModule && "LowerModule shouldn't be null");
diff --git a/clang/test/CIR/Transforms/abi-lowering/spirv-calling-conv.cir b/clang/test/CIR/Transforms/abi-lowering/spirv-calling-conv.cir
index 2a8bc04f8f924..d47c22d7e84b6 100644
--- a/clang/test/CIR/Transforms/abi-lowering/spirv-calling-conv.cir
+++ b/clang/test/CIR/Transforms/abi-lowering/spirv-calling-conv.cir
@@ -168,24 +168,3 @@ module attributes {
 
   // CHECK: cir.func{{.*}} @kernel_ptr(%arg0: !cir.ptr<!s32i, target_address_space(1)>
 }
-
-// -----
-
-// Without a cir.triple the target cannot be rebuilt, so the default address
-// space falls back to 0.
-
-!s32i = !cir.int<s, 32>
-
-module attributes {
-  dlti.dl_spec = #dlti.dl_spec<
-    #dlti.dl_entry<i32, dense<32>: vector<2xi64>>,
-    #dlti.dl_entry<i64, dense<64>: vector<2xi64>>>
-} {
-
-  // The default address space is 0, so a private kernel pointer is coerced.
-  cir.func @kernel_ptr_no_triple(%arg0: !cir.ptr<!s32i>) cc(spir_kernel) requires_call_conv_lowering {
-    cir.return
-  }
-
-  // CHECK: cir.func{{.*}} @kernel_ptr_no_triple(%arg0: !cir.ptr<!s32i, target_address_space(1)>
-}
diff --git a/clang/test/CIR/Transforms/abi-lowering/spirv-intel.cir b/clang/test/CIR/Transforms/abi-lowering/spirv-intel.cir
new file mode 100644
index 0000000000000..4c3d530b056a5
--- /dev/null
+++ b/clang/test/CIR/Transforms/abi-lowering/spirv-intel.cir
@@ -0,0 +1,85 @@
+// RUN: cir-opt %s -cir-call-conv-lowering=target=spirv | FileCheck %s
+
+// Checks SPIR-V classification for the Intel vendor without a device
+// compilation. The Intel vendor alone makes the generic address space (4) the
+// default one, as in classic codegen, so:
+// - only a kernel pointer in the default (generic) address space is passed as
+//   a CrossWorkgroup (1) pointer; pointers in any other address space, such as
+//   private (0), are left alone;
+// - a record that cannot be copied is returned through a generic sret pointer;
+// - without the device flag, a record that cannot be copied is passed to a
+//   kernel indirectly but not byval.
+
+!s32i = !cir.int<s, 32>
+!rec_S = !cir.struct<"S" {data !s32i, data !cir.float}>
+!rec_NT = !cir.struct<"NT" {data !s32i}>
+
+module attributes {
+  cir.triple = "spirv64-intel",
+  cir.lowering_options = #cir.lowering_options<
+    exceptions = false, threadsafe_statics = true, cuda = false,
+    cuda_is_device = false, hip = false, hlsl = false, sycl_is_device = false,
+    gpu_rdc = false, openmp = false, openmp_is_target_device = false,
+    clang_abi_compat = 17, long_double_size = 0, enable_noundef_attrs = true,
+    no_trapping_math = true, ssp_buffer_size = 8>,
+  cir.record_layouts = {
+    S = #cir.record_layout<arg_passing_kind = can_pass_in_regs,
+      has_trivial_dtor = true, record_align = 4>,
+    NT = #cir.record_layout<arg_passing_kind = cannot_pass_in_regs,
+      has_trivial_dtor = false, record_align = 4>},
+  dlti.dl_spec = #dlti.dl_spec<
+    #dlti.dl_entry<i32, dense<32>: vector<2xi64>>,
+    #dlti.dl_entry<i64, dense<64>: vector<2xi64>>>
+} {
+
+  // A generic pointer is passed as a CrossWorkgroup pointer and cast back to
+  // generic before the body's spill reads it.
+  cir.func @kernel_ptr(%arg0: !cir.ptr<!s32i, target_address_space(4)>) cc(spir_kernel) requires_call_conv_lowering {
+    %0 = cir.alloca "p" align(8) init : !cir.ptr<!cir.ptr<!s32i, target_address_space(4)>>
+    cir.store %arg0, %0 : !cir.ptr<!s32i, target_address_space(4)>, !cir.ptr<!cir.ptr<!s32i, target_address_space(4)>>
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @kernel_ptr(%[[ARG:[^:]+]]: !cir.ptr<!s32i, target_address_space(1)>{{.*}}cc(spir_kernel)
+  // CHECK-NEXT:   %[[GEN:.*]] = cir.cast address_space %[[ARG]] : !cir.ptr<!s32i, target_address_space(1)> -> !cir.ptr<!s32i, target_address_space(4)>
+  // CHECK:        cir.store %[[GEN]], %{{.*}} : !cir.ptr<!s32i, target_address_space(4)>
+
+  // A private pointer is not the default one here, so it is left alone.
+  cir.func @kernel_private_ptr(%arg0: !cir.ptr<!s32i>) cc(spir_kernel) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @kernel_private_ptr(%arg0: !cir.ptr<!s32i>{{.*}}cc(spir_kernel)
+  // CHECK-NOT:    cir.cast
+
+  // A copyable aggregate is byval through the default rule.
+  cir.func @kernel_rec(%arg0: !rec_S) cc(spir_kernel) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @kernel_rec(%{{.*}}: !cir.ptr<!rec_S> {
+  // CHECK-SAME:   llvm.byval = !rec_S
+
+  // Without the device flag, a record that cannot be copied is passed
+  // indirectly, not byval.
+  cir.func @kernel_nt(%arg0: !rec_NT) cc(spir_kernel) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK:      cir.func{{.*}} @kernel_nt(%{{.*}}: !cir.ptr<!rec_NT>
+  // CHECK-NOT:    llvm.byval
+  // CHECK:      cir.func{{.*}} @device_ptr
+
+  // Only kernels are coerced: a device function keeps the generic pointer.
+  cir.func @device_ptr(%arg0: !cir.ptr<!s32i, target_address_space(4)>) cc(spir_function) requires_call_conv_lowering {
+    cir.return
+  }
+
+  // CHECK-SAME: (%arg0: !cir.ptr<!s32i, target_address_space(4)>{{.*}}cc(spir_function)
+
+  // A record that cannot be copied is returned through an sret pointer in the
+  // generic default address space.
+  cir.func private @make_nt() -> !rec_NT cc(spir_function) requires_call_conv_lowering
+
+  // CHECK: cir.func private @make_nt(!cir.ptr<!rec_NT, target_address_space(4)> {llvm.align = 4 : i64, llvm.dead_on_unwind, llvm.sret = !rec_NT, llvm.writable}) cc(spir_function)
+}
diff --git a/clang/test/CIR/Transforms/abi-lowering/spirv-kernel-no-device.cir b/clang/test/CIR/Transforms/abi-lowering/spirv-kernel-no-device.cir
index 49615aa3a15f6..2aab3a3448e30 100644
--- a/clang/test/CIR/Transforms/abi-lowering/spirv-kernel-no-device.cir
+++ b/clang/test/CIR/Transforms/abi-lowering/spirv-kernel-no-device.cir
@@ -3,8 +3,9 @@
 // Checks SPIR-V kernel argument classification outside a device compilation.
 // The default address space is then 0, and kernel pointer coercion follows the
 // default address space rather than the device flag, as in classic codegen:
-// - a default (address space 0) pointer is still passed as a CrossWorkgroup
-//   (1) pointer, and a generic (4) one is left alone;
+// - only a kernel pointer in the default (here private, 0) address space is
+//   passed as a CrossWorkgroup (1) pointer; pointers in any other address
+//   space, such as generic (4), are left alone;
 // - without the device flag, a record that cannot be copied is passed
 //   indirectly but not byval. Copyable aggregates are byval either way.
 



More information about the cfe-commits mailing list