[clang] [CIR][NFC] Extract record-rebuilding type converter from CXXABILowering (PR #228599)
https://github.com/koparasy closed https://github.com/llvm/llvm-project/pull/228599 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [CIR][NFC] Extract record-rebuilding type converter from CXXABILowering (PR #228599)
https://github.com/koparasy auto_merge_enabled https://github.com/llvm/llvm-project/pull/228599 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [CIR][NFC] Extract record-rebuilding type converter from CXXABILowering (PR #228599)
https://github.com/koparasy updated
https://github.com/llvm/llvm-project/pull/228599
>From ebe1d8c77509b21bf9a7ae44b3101f0e0f5c8be5 Mon Sep 17 00:00:00 2001
From: Konstantinos Parasyris
Date: Fri, 2 Oct 2026 14:43:39 -0700
Subject: [PATCH 1/2] [CIR][NFC] Extract record-rebuilding type converter from
CXXABILowering
CXXABILowering's type converter rebuilds identified records whose member
types change. Because identified records cannot be modified in place, each
one is recreated under a temporary name, recursive records are resolved with
a per-thread conversion stack, and the original names are restored once the
conversion is done.
This patch moves the `CIRABITypeConverter`` machinery into a reusable
base class, `cir::RecordRewritingTypeConverter`.
`CIRABITypeConverter` now derives from it and only adds the C++ ABI specific
conversions (data member and method types).
The base class also gets a `shouldConvertRecord(RecordType)` hook, which
defaults to `true`. CXXABILowering does not override it, so its behavior is
unchanged.
TargetLowering needs the same record rebuilding to convert language address
spaces nested in record members. Today the following OpenCL code fails on
SPIR-V:
```
struct S { __global int *p; int n; };
kernel void k(__global int *g) {
struct S s;
s.p = g;
*s.p = 1;
}
error: member type mismatch
```
TargetLowering converts the `get_member` result to
`!cir.ptr`, but the field type inside `!rec_S`
stays `lang_address_space(offload_global)`, so the verifier rejects the op. The
same issue affects SYCL device code.
A follow-up will make TargetLowering's type converter derive from this class
and use `shouldConvertRecord` to rebuild only the records that contain
language address spaces. Records without them (e.g. all CUDA/HIP records) stay
untouched, so the operations using them stay legal.
---
.../lib/CIR/Dialect/Transforms/CMakeLists.txt | 1 +
.../CIR/Dialect/Transforms/CXXABILowering.cpp | 184 +
.../Transforms/RecordTypeConverter.cpp| 185 ++
.../Dialect/Transforms/RecordTypeConverter.h | 76 +++
4 files changed, 266 insertions(+), 180 deletions(-)
create mode 100644 clang/lib/CIR/Dialect/Transforms/RecordTypeConverter.cpp
create mode 100644 clang/lib/CIR/Dialect/Transforms/RecordTypeConverter.h
diff --git a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
index 3678fc0b253849..2d6d04fb24abc5 100644
--- a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
+++ b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
@@ -14,6 +14,7 @@ add_clang_library(MLIRCIRTransforms
GotoSolver.cpp
IdiomRecognizer.cpp
LibOpt.cpp
+ RecordTypeConverter.cpp
DEPENDS
MLIRCIRPassIncGen
diff --git a/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
b/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
index 9a0e40bd4f5068..735146b04c64f6 100644
--- a/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
@@ -7,6 +7,7 @@
//===--===//
#include "PassDetail.h"
+#include "RecordTypeConverter.h"
#include "TargetLowering/LowerModule.h"
#include "mlir/Dialect/OpenACC/OpenACCOpsDialect.h.inc"
@@ -24,7 +25,6 @@
#include "clang/CIR/Dialect/Transforms/CIRTransformUtils.h"
#include "clang/CIR/MissingFeatures.h"
-#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/TypeSwitch.h"
using namespace mlir;
@@ -813,159 +813,12 @@ mlir::LogicalResult
CIRVTableGetTypeInfoOpABILowering::matchAndRewrite(
namespace {
// A small type to handle type conversion for the the CXXABILoweringPass.
// Even though this is a CIR-to-CIR pass, we are eliminating some CIR types.
-// Most importantly, this pass solves recursive type conversion problems by
-// keeping a call stack.
-class CIRABITypeConverter : public mlir::TypeConverter {
-
- mlir::MLIRContext &context;
-
- // Recursive structure detection.
- // We store one entry per thread here, and rely on locking. This works the
- // same way as the LLVM-IR lowering does it, which has a similar problem.
- DenseMap>>
- conversionCallStack;
- llvm::sys::SmartRWMutex callStackMutex;
-
- // In order to let us 'change the names' back after the fact, we collect them
- // along the way. They should only be added/accessed via the thread-safe
- // functions below.
- llvm::SmallVector convertedRecordTypes;
- llvm::sys::SmartRWMutex recordTypeMutex;
-
- // This provides a stack for the RecordTypes being processed on the current
- // thread, which lets us solve recursive conversions. This implementation is
- // cribbed from the LLVMTypeConverter which solves a similar but not
identical
- // problem.
- SmallVector &getCurrentThreadRecursiveStack() {
-{
- // Most of the time, the entry already exists in the map.
- std::shared_lock lock(callStackMutex,
-
[clang] [CIR][NFC] Extract record-rebuilding type converter from CXXABILowering (PR #228599)
https://github.com/koparasy updated
https://github.com/llvm/llvm-project/pull/228599
>From 95063ffc3fee5780cc29761119f709fcec3e678f Mon Sep 17 00:00:00 2001
From: Konstantinos Parasyris
Date: Fri, 2 Oct 2026 14:43:39 -0700
Subject: [PATCH 1/2] [CIR][NFC] Extract record-rebuilding type converter from
CXXABILowering
CXXABILowering's type converter rebuilds identified records whose member
types change. Because identified records cannot be modified in place, each
one is recreated under a temporary name, recursive records are resolved with
a per-thread conversion stack, and the original names are restored once the
conversion is done.
This patch moves the `CIRABITypeConverter`` machinery into a reusable
base class, `cir::RecordRewritingTypeConverter`.
`CIRABITypeConverter` now derives from it and only adds the C++ ABI specific
conversions (data member and method types).
The base class also gets a `shouldConvertRecord(RecordType)` hook, which
defaults to `true`. CXXABILowering does not override it, so its behavior is
unchanged.
TargetLowering needs the same record rebuilding to convert language address
spaces nested in record members. Today the following OpenCL code fails on
SPIR-V:
```
struct S { __global int *p; int n; };
kernel void k(__global int *g) {
struct S s;
s.p = g;
*s.p = 1;
}
error: member type mismatch
```
TargetLowering converts the `get_member` result to
`!cir.ptr`, but the field type inside `!rec_S`
stays `lang_address_space(offload_global)`, so the verifier rejects the op. The
same issue affects SYCL device code.
A follow-up will make TargetLowering's type converter derive from this class
and use `shouldConvertRecord` to rebuild only the records that contain
language address spaces. Records without them (e.g. all CUDA/HIP records) stay
untouched, so the operations using them stay legal.
---
.../lib/CIR/Dialect/Transforms/CMakeLists.txt | 1 +
.../CIR/Dialect/Transforms/CXXABILowering.cpp | 184 +
.../Transforms/RecordTypeConverter.cpp| 185 ++
.../Dialect/Transforms/RecordTypeConverter.h | 76 +++
4 files changed, 266 insertions(+), 180 deletions(-)
create mode 100644 clang/lib/CIR/Dialect/Transforms/RecordTypeConverter.cpp
create mode 100644 clang/lib/CIR/Dialect/Transforms/RecordTypeConverter.h
diff --git a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
index 3678fc0b253849..2d6d04fb24abc5 100644
--- a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
+++ b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
@@ -14,6 +14,7 @@ add_clang_library(MLIRCIRTransforms
GotoSolver.cpp
IdiomRecognizer.cpp
LibOpt.cpp
+ RecordTypeConverter.cpp
DEPENDS
MLIRCIRPassIncGen
diff --git a/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
b/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
index 9a0e40bd4f5068..735146b04c64f6 100644
--- a/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
@@ -7,6 +7,7 @@
//===--===//
#include "PassDetail.h"
+#include "RecordTypeConverter.h"
#include "TargetLowering/LowerModule.h"
#include "mlir/Dialect/OpenACC/OpenACCOpsDialect.h.inc"
@@ -24,7 +25,6 @@
#include "clang/CIR/Dialect/Transforms/CIRTransformUtils.h"
#include "clang/CIR/MissingFeatures.h"
-#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/TypeSwitch.h"
using namespace mlir;
@@ -813,159 +813,12 @@ mlir::LogicalResult
CIRVTableGetTypeInfoOpABILowering::matchAndRewrite(
namespace {
// A small type to handle type conversion for the the CXXABILoweringPass.
// Even though this is a CIR-to-CIR pass, we are eliminating some CIR types.
-// Most importantly, this pass solves recursive type conversion problems by
-// keeping a call stack.
-class CIRABITypeConverter : public mlir::TypeConverter {
-
- mlir::MLIRContext &context;
-
- // Recursive structure detection.
- // We store one entry per thread here, and rely on locking. This works the
- // same way as the LLVM-IR lowering does it, which has a similar problem.
- DenseMap>>
- conversionCallStack;
- llvm::sys::SmartRWMutex callStackMutex;
-
- // In order to let us 'change the names' back after the fact, we collect them
- // along the way. They should only be added/accessed via the thread-safe
- // functions below.
- llvm::SmallVector convertedRecordTypes;
- llvm::sys::SmartRWMutex recordTypeMutex;
-
- // This provides a stack for the RecordTypes being processed on the current
- // thread, which lets us solve recursive conversions. This implementation is
- // cribbed from the LLVMTypeConverter which solves a similar but not
identical
- // problem.
- SmallVector &getCurrentThreadRecursiveStack() {
-{
- // Most of the time, the entry already exists in the map.
- std::shared_lock lock(callStackMutex,
-
[clang] [CIR][NFC] Extract record-rebuilding type converter from CXXABILowering (PR #228599)
https://github.com/koparasy updated
https://github.com/llvm/llvm-project/pull/228599
>From 95063ffc3fee5780cc29761119f709fcec3e678f Mon Sep 17 00:00:00 2001
From: Konstantinos Parasyris
Date: Fri, 2 Oct 2026 14:43:39 -0700
Subject: [PATCH 1/2] [CIR][NFC] Extract record-rebuilding type converter from
CXXABILowering
CXXABILowering's type converter rebuilds identified records whose member
types change. Because identified records cannot be modified in place, each
one is recreated under a temporary name, recursive records are resolved with
a per-thread conversion stack, and the original names are restored once the
conversion is done.
This patch moves the `CIRABITypeConverter`` machinery into a reusable
base class, `cir::RecordRewritingTypeConverter`.
`CIRABITypeConverter` now derives from it and only adds the C++ ABI specific
conversions (data member and method types).
The base class also gets a `shouldConvertRecord(RecordType)` hook, which
defaults to `true`. CXXABILowering does not override it, so its behavior is
unchanged.
TargetLowering needs the same record rebuilding to convert language address
spaces nested in record members. Today the following OpenCL code fails on
SPIR-V:
```
struct S { __global int *p; int n; };
kernel void k(__global int *g) {
struct S s;
s.p = g;
*s.p = 1;
}
error: member type mismatch
```
TargetLowering converts the `get_member` result to
`!cir.ptr`, but the field type inside `!rec_S`
stays `lang_address_space(offload_global)`, so the verifier rejects the op. The
same issue affects SYCL device code.
A follow-up will make TargetLowering's type converter derive from this class
and use `shouldConvertRecord` to rebuild only the records that contain
language address spaces. Records without them (e.g. all CUDA/HIP records) stay
untouched, so the operations using them stay legal.
---
.../lib/CIR/Dialect/Transforms/CMakeLists.txt | 1 +
.../CIR/Dialect/Transforms/CXXABILowering.cpp | 184 +
.../Transforms/RecordTypeConverter.cpp| 185 ++
.../Dialect/Transforms/RecordTypeConverter.h | 76 +++
4 files changed, 266 insertions(+), 180 deletions(-)
create mode 100644 clang/lib/CIR/Dialect/Transforms/RecordTypeConverter.cpp
create mode 100644 clang/lib/CIR/Dialect/Transforms/RecordTypeConverter.h
diff --git a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
index 3678fc0b25384..2d6d04fb24abc 100644
--- a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
+++ b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
@@ -14,6 +14,7 @@ add_clang_library(MLIRCIRTransforms
GotoSolver.cpp
IdiomRecognizer.cpp
LibOpt.cpp
+ RecordTypeConverter.cpp
DEPENDS
MLIRCIRPassIncGen
diff --git a/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
b/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
index 9a0e40bd4f506..735146b04c64f 100644
--- a/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
@@ -7,6 +7,7 @@
//===--===//
#include "PassDetail.h"
+#include "RecordTypeConverter.h"
#include "TargetLowering/LowerModule.h"
#include "mlir/Dialect/OpenACC/OpenACCOpsDialect.h.inc"
@@ -24,7 +25,6 @@
#include "clang/CIR/Dialect/Transforms/CIRTransformUtils.h"
#include "clang/CIR/MissingFeatures.h"
-#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/TypeSwitch.h"
using namespace mlir;
@@ -813,159 +813,12 @@ mlir::LogicalResult
CIRVTableGetTypeInfoOpABILowering::matchAndRewrite(
namespace {
// A small type to handle type conversion for the the CXXABILoweringPass.
// Even though this is a CIR-to-CIR pass, we are eliminating some CIR types.
-// Most importantly, this pass solves recursive type conversion problems by
-// keeping a call stack.
-class CIRABITypeConverter : public mlir::TypeConverter {
-
- mlir::MLIRContext &context;
-
- // Recursive structure detection.
- // We store one entry per thread here, and rely on locking. This works the
- // same way as the LLVM-IR lowering does it, which has a similar problem.
- DenseMap>>
- conversionCallStack;
- llvm::sys::SmartRWMutex callStackMutex;
-
- // In order to let us 'change the names' back after the fact, we collect them
- // along the way. They should only be added/accessed via the thread-safe
- // functions below.
- llvm::SmallVector convertedRecordTypes;
- llvm::sys::SmartRWMutex recordTypeMutex;
-
- // This provides a stack for the RecordTypes being processed on the current
- // thread, which lets us solve recursive conversions. This implementation is
- // cribbed from the LLVMTypeConverter which solves a similar but not
identical
- // problem.
- SmallVector &getCurrentThreadRecursiveStack() {
-{
- // Most of the time, the entry already exists in the map.
- std::shared_lock lock(callStackMutex,
-
[clang] [CIR][NFC] Extract record-rebuilding type converter from CXXABILowering (PR #228599)
@@ -0,0 +1,185 @@
+//===- RecordTypeConverter.cpp - Record-rebuilding type converter
-===//
+//
+// 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
+//
+//===--===//
+
+#include "RecordTypeConverter.h"
+
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/ScopeExit.h"
+#include "llvm/Support/Threading.h"
+
+#include
+#include
+
+using namespace cir;
+
+RecordRewritingTypeConverter::RecordRewritingTypeConverter(
+mlir::MLIRContext &context)
+: context(context) {
+ addConversion([&](mlir::Type type) -> mlir::Type { return type; });
+ // This is necessary in order to convert CIR pointer types that are pointing
+ // to CIR types that are being converted.
+ addConversion([&](cir::PointerType type) -> mlir::Type {
+mlir::Type loweredPointeeType = convertType(type.getPointee());
+if (!loweredPointeeType)
+ return {};
+return cir::PointerType::get(type.getContext(), loweredPointeeType,
+ type.getAddrSpace());
+ });
+ addConversion([&](cir::ArrayType type) -> mlir::Type {
+mlir::Type loweredElementType = convertType(type.getElementType());
+if (!loweredElementType)
+ return {};
+return cir::ArrayType::get(loweredElementType, type.getSize());
+ });
+ // This is necessary in order to convert CIR function types that have
+ // argument or return types that use CIR types that are being converted.
+ addConversion([&](cir::FuncType type) -> mlir::Type {
+llvm::SmallVector loweredInputTypes;
+loweredInputTypes.reserve(type.getNumInputs());
+if (mlir::failed(convertTypes(type.getInputs(), loweredInputTypes)))
+ return {};
+
+mlir::Type loweredReturnType = convertType(type.getReturnType());
+if (!loweredReturnType)
+ return {};
+
+return cir::FuncType::get(loweredInputTypes, loweredReturnType,
+ /*isVarArg=*/type.getVarArg());
+ });
+ addConversion([&](cir::StructType type) -> mlir::Type {
+return convertRecordType(type);
+ });
+ addConversion([&](cir::UnionType type) -> mlir::Type {
+return convertRecordType(type);
+ });
+}
+
+void RecordRewritingTypeConverter::restoreRecordTypeNames() {
+ std::unique_lock lock(recordTypeMutex);
+
+ for (auto rt : convertedRecordTypes)
+rt.removeABIConversionNamePrefix();
+}
+
+// This provides a stack for the RecordTypes being processed on the current
+// thread, which lets us solve recursive conversions. This implementation is
+// cribbed from the LLVMTypeConverter which solves a similar but not identical
+// problem.
+llvm::SmallVector &
+RecordRewritingTypeConverter::getCurrentThreadRecursiveStack() {
+ {
+// Most of the time, the entry already exists in the map.
+std::shared_lock lock(callStackMutex,
+std::defer_lock);
+if (context.isMultithreadingEnabled())
+ lock.lock();
+auto recursiveStack = conversionCallStack.find(llvm::get_threadid());
+if (recursiveStack != conversionCallStack.end())
+ return *recursiveStack->second;
+ }
+
+ // First time this thread gets here, we have to get an exclusive access to
+ // insert in the map
+ std::unique_lock lock(callStackMutex);
+ auto recursiveStackInserted = conversionCallStack.insert(
+ std::make_pair(llvm::get_threadid(),
+ std::make_unique>()));
+ return *recursiveStackInserted.first->second;
+}
+
+void RecordRewritingTypeConverter::addConvertedRecordType(cir::RecordType rt) {
+ std::unique_lock lock(recordTypeMutex);
+ convertedRecordTypes.push_back(rt);
+}
+
+llvm::SmallVector
+RecordRewritingTypeConverter::convertRecordMemberTypes(cir::RecordType type) {
+ llvm::SmallVector loweredMemberTypes;
+ loweredMemberTypes.reserve(type.getNumElements());
+
+ if (mlir::failed(convertTypes(type.getMembers(), loweredMemberTypes)))
+return {};
+
+ return loweredMemberTypes;
+}
+
+cir::RecordType
+RecordRewritingTypeConverter::convertRecordType(cir::RecordType type) {
+ if (!shouldConvertRecord(type))
koparasy wrote:
3f6587f
https://github.com/llvm/llvm-project/pull/228599
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [CIR][NFC] Extract record-rebuilding type converter from CXXABILowering (PR #228599)
https://github.com/koparasy edited https://github.com/llvm/llvm-project/pull/228599 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [CIR][NFC] Extract record-rebuilding type converter from CXXABILowering (PR #228599)
llvmorg-github-actions[bot] wrote:
@llvm/pr-subscribers-clangir
Author: Konstantinos Parasyris (koparasy)
Changes
CXXABILowering's type converter rebuilds identified records whose member types
change. Because identified records cannot be modified in place, each one is
recreated under a temporary name, recursive records are resolved with a
per-thread conversion stack, and the original names are restored once the
conversion is done.
This patch moves the `CIRABITypeConverter` machinery into a reusable base
class, `cir::RecordRewritingTypeConverter`.
`CIRABITypeConverter` now derives from it and only adds the C++ ABI specific
conversions (data member and method types).
The base class also gets a `shouldConvertRecord(RecordType)` hook, which
defaults to `true`. CXXABILowering does not override it, so its behavior is
unchanged.
TargetLowering needs the same record rebuilding to convert language address
spaces nested in record members. Today the following OpenCL code fails on
SPIR-V:
```
struct S { __global int *p; int n; };
kernel void k(__global int *g) {
struct S s;
s.p = g;
*s.p = 1;
}
```
> error: member type mismatch
TargetLowering converts the `get_member` result to `!cir.ptr`, but the field type inside `!rec_S` stays
`lang_address_space(offload_global)`, so the verifier rejects the op. The same
issue affects SYCL device code.
A follow-up will make TargetLowering's type converter derive from this class
and use `shouldConvertRecord` to rebuild only the records that contain language
address spaces. Records without them (e.g. all CUDA/HIP records) stay
untouched, so the operations using them stay legal.
---
Patch is 20.73 KiB, truncated to 20.00 KiB below, full version:
https://github.com/llvm/llvm-project/pull/228599.diff
4 Files Affected:
- (modified) clang/lib/CIR/Dialect/Transforms/CMakeLists.txt (+1)
- (modified) clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp (+4-180)
- (added) clang/lib/CIR/Dialect/Transforms/RecordTypeConverter.cpp (+185)
- (added) clang/lib/CIR/Dialect/Transforms/RecordTypeConverter.h (+76)
``diff
diff --git a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
index 3678fc0b25384..2d6d04fb24abc 100644
--- a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
+++ b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
@@ -14,6 +14,7 @@ add_clang_library(MLIRCIRTransforms
GotoSolver.cpp
IdiomRecognizer.cpp
LibOpt.cpp
+ RecordTypeConverter.cpp
DEPENDS
MLIRCIRPassIncGen
diff --git a/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
b/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
index 9a0e40bd4f506..735146b04c64f 100644
--- a/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
@@ -7,6 +7,7 @@
//===--===//
#include "PassDetail.h"
+#include "RecordTypeConverter.h"
#include "TargetLowering/LowerModule.h"
#include "mlir/Dialect/OpenACC/OpenACCOpsDialect.h.inc"
@@ -24,7 +25,6 @@
#include "clang/CIR/Dialect/Transforms/CIRTransformUtils.h"
#include "clang/CIR/MissingFeatures.h"
-#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/TypeSwitch.h"
using namespace mlir;
@@ -813,159 +813,12 @@ mlir::LogicalResult
CIRVTableGetTypeInfoOpABILowering::matchAndRewrite(
namespace {
// A small type to handle type conversion for the the CXXABILoweringPass.
// Even though this is a CIR-to-CIR pass, we are eliminating some CIR types.
-// Most importantly, this pass solves recursive type conversion problems by
-// keeping a call stack.
-class CIRABITypeConverter : public mlir::TypeConverter {
-
- mlir::MLIRContext &context;
-
- // Recursive structure detection.
- // We store one entry per thread here, and rely on locking. This works the
- // same way as the LLVM-IR lowering does it, which has a similar problem.
- DenseMap>>
- conversionCallStack;
- llvm::sys::SmartRWMutex callStackMutex;
-
- // In order to let us 'change the names' back after the fact, we collect them
- // along the way. They should only be added/accessed via the thread-safe
- // functions below.
- llvm::SmallVector convertedRecordTypes;
- llvm::sys::SmartRWMutex recordTypeMutex;
-
- // This provides a stack for the RecordTypes being processed on the current
- // thread, which lets us solve recursive conversions. This implementation is
- // cribbed from the LLVMTypeConverter which solves a similar but not
identical
- // problem.
- SmallVector &getCurrentThreadRecursiveStack() {
-{
- // Most of the time, the entry already exists in the map.
- std::shared_lock lock(callStackMutex,
- std::defer_lock);
- if (context.isMultithreadingEnabled())
-lock.lock();
- auto recursiveStack =
[clang] [CIR][NFC] Extract record-rebuilding type converter from CXXABILowering (PR #228599)
https://github.com/koparasy created
https://github.com/llvm/llvm-project/pull/228599
CXXABILowering's type converter rebuilds identified records whose member types
change. Because identified records cannot be modified in place, each one is
recreated under a temporary name, recursive records are resolved with a
per-thread conversion stack, and the original names are restored once the
conversion is done.
This patch moves the `CIRABITypeConverter` machinery into a reusable base
class, `cir::RecordRewritingTypeConverter`.
`CIRABITypeConverter` now derives from it and only adds the C++ ABI specific
conversions (data member and method types).
The base class also gets a `shouldConvertRecord(RecordType)` hook, which
defaults to `true`. CXXABILowering does not override it, so its behavior is
unchanged.
TargetLowering needs the same record rebuilding to convert language address
spaces nested in record members. Today the following OpenCL code fails on
SPIR-V:
```
struct S { __global int *p; int n; };
kernel void k(__global int *g) {
struct S s;
s.p = g;
*s.p = 1;
}
```
> error: member type mismatch
TargetLowering converts the `get_member` result to `!cir.ptr`, but the field type inside `!rec_S` stays
`lang_address_space(offload_global)`, so the verifier rejects the op. The same
issue affects SYCL device code.
A follow-up will make TargetLowering's type converter derive from this class
and use `shouldConvertRecord` to rebuild only the records that contain language
address spaces. Records without them (e.g. all CUDA/HIP records) stay
untouched, so the operations using them stay legal.
>From 95063ffc3fee5780cc29761119f709fcec3e678f Mon Sep 17 00:00:00 2001
From: Konstantinos Parasyris
Date: Fri, 2 Oct 2026 14:43:39 -0700
Subject: [PATCH] [CIR][NFC] Extract record-rebuilding type converter from
CXXABILowering
CXXABILowering's type converter rebuilds identified records whose member
types change. Because identified records cannot be modified in place, each
one is recreated under a temporary name, recursive records are resolved with
a per-thread conversion stack, and the original names are restored once the
conversion is done.
This patch moves the `CIRABITypeConverter`` machinery into a reusable
base class, `cir::RecordRewritingTypeConverter`.
`CIRABITypeConverter` now derives from it and only adds the C++ ABI specific
conversions (data member and method types).
The base class also gets a `shouldConvertRecord(RecordType)` hook, which
defaults to `true`. CXXABILowering does not override it, so its behavior is
unchanged.
TargetLowering needs the same record rebuilding to convert language address
spaces nested in record members. Today the following OpenCL code fails on
SPIR-V:
```
struct S { __global int *p; int n; };
kernel void k(__global int *g) {
struct S s;
s.p = g;
*s.p = 1;
}
error: member type mismatch
```
TargetLowering converts the `get_member` result to
`!cir.ptr`, but the field type inside `!rec_S`
stays `lang_address_space(offload_global)`, so the verifier rejects the op. The
same issue affects SYCL device code.
A follow-up will make TargetLowering's type converter derive from this class
and use `shouldConvertRecord` to rebuild only the records that contain
language address spaces. Records without them (e.g. all CUDA/HIP records) stay
untouched, so the operations using them stay legal.
---
.../lib/CIR/Dialect/Transforms/CMakeLists.txt | 1 +
.../CIR/Dialect/Transforms/CXXABILowering.cpp | 184 +
.../Transforms/RecordTypeConverter.cpp| 185 ++
.../Dialect/Transforms/RecordTypeConverter.h | 76 +++
4 files changed, 266 insertions(+), 180 deletions(-)
create mode 100644 clang/lib/CIR/Dialect/Transforms/RecordTypeConverter.cpp
create mode 100644 clang/lib/CIR/Dialect/Transforms/RecordTypeConverter.h
diff --git a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
index 3678fc0b25384..2d6d04fb24abc 100644
--- a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
+++ b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
@@ -14,6 +14,7 @@ add_clang_library(MLIRCIRTransforms
GotoSolver.cpp
IdiomRecognizer.cpp
LibOpt.cpp
+ RecordTypeConverter.cpp
DEPENDS
MLIRCIRPassIncGen
diff --git a/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
b/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
index 9a0e40bd4f506..735146b04c64f 100644
--- a/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CXXABILowering.cpp
@@ -7,6 +7,7 @@
//===--===//
#include "PassDetail.h"
+#include "RecordTypeConverter.h"
#include "TargetLowering/LowerModule.h"
#include "mlir/Dialect/OpenACC/OpenACCOpsDialect.h.inc"
@@ -24,7 +25,6 @@
#include "clang/CIR/Dialect/Transforms/CIRTransformUtils.h"
#include "clang/CIR/MissingFeatures.h"
