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

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 12 07:51:41 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangir

@llvm/pr-subscribers-clang

Author: Qi Ye (Qi-Ye-079)

<details>
<summary>Changes</summary>

Add a SPIR-V target to `CallConvLowering` (`-cir-call-conv-lowering=target=spirv`), classified by `SPIRVTargetInfo` in the LLVM ABI library.

Ensures the lowering pass correctly follow SPIR-V-specific classification rules implemented by #<!-- -->224699:
- A kernel pointer argument in the default address space is passed as a `CrossWorkgroup` (addrspace 1) pointer and cast back in the kernel.
- In device compilations, a kernel aggregate argument is passed `byval` in addrspace 0.
- A record that cannot be copied is returned through an `sret` pointer in the default address space.
Everything else, including the remaining kernel arguments, follows the default classification rules `DefaultTargetInfo`.

The classifier gets the same inputs classic codegen passes under `-fexperimental-abi-lowering`: the target address space of `LangAS::Default`, from the `clang::TargetInfo` that `LowerModule` rebuilds from `cir.triple` and `cir.lowering_options` (0 without a triple), and `LangOptions::isTargetDevice()`. To support this:
- `LowerModule` now restores `LangOptions::SYCLIsDevice` from `cir.lowering_options`, so the generic address space is SYCL's default.
- `convertCallingConv` maps `cir::CallingConv::SpirKernel` to `llvm::CallingConv::SPIR_KERNEL`.

Notes:
- Only the spirv, spirv32 and spirv64 triples are covered, matching #<!-- -->224699.
- Selecting this target in the clang pipeline (clang -fclangir) will follow in a separate PR.

Part of #<!-- -->233633.

Assisted by: Claude Opus 5.5

---

Patch is 30.68 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/241531.diff


12 Files Affected:

- (modified) clang/include/clang/CIR/Dialect/Passes.h (+1-1) 
- (modified) clang/include/clang/CIR/Dialect/Passes.td (+3-1) 
- (modified) clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp (+4-2) 
- (modified) clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.h (+4) 
- (added) clang/lib/CIR/Dialect/Transforms/CIRABIClassifierSPIRV.cpp (+75) 
- (modified) clang/lib/CIR/Dialect/Transforms/CMakeLists.txt (+1) 
- (modified) clang/lib/CIR/Dialect/Transforms/CallConvLoweringPass.cpp (+2) 
- (modified) clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerModule.cpp (+1) 
- (added) clang/test/CIR/Transforms/abi-lowering/spirv-calling-conv.cir (+191) 
- (added) clang/test/CIR/Transforms/abi-lowering/spirv-indirect.cir (+104) 
- (added) clang/test/CIR/Transforms/abi-lowering/spirv-kernel-no-device.cir (+73) 
- (added) clang/test/CIR/Transforms/abi-lowering/spirv-kernel-sycl.cir (+103) 


``````````diff
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 2855b5fbe349c..01a6e42b80375 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRABIClassifier.cpp
@@ -771,11 +771,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 30d1884318b11..155dc37e4dffa 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_lowerin...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/241531


More information about the cfe-commits mailing list