Skip to content

Commit cffc9ec

Browse files
authored
[SYCL][NATIVECPU] Fix TargetInfo initialization (DataLayoutString) (#19711)
Some clang targets (for example for Arm) may not initialize `DataLayoutString` in their TargetInfo constructor, which meant calls `getDataLayoutString` right after the constructor cause an assert. This PR defers the initialization of the NativeCPU TargetInfo to after the call of `handleTargetFeatures` of the host cpu Target info, which on Arm initializes `DataLayoutString`, but may also catch more initializations.
1 parent ea65509 commit cffc9ec

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

clang/lib/Basic/Targets/NativeCPU.cpp

100644100755
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ NativeCPUTargetInfo::NativeCPUTargetInfo(const llvm::Triple &Triple,
6262
resetDataLayout("e");
6363
} else {
6464
HostTarget = AllocateTarget(HostTriple, Opts);
65-
copyAuxTarget(&*HostTarget);
66-
resetDataLayout(HostTarget->getDataLayoutString());
6765
}
6866
}
6967

@@ -73,3 +71,17 @@ void NativeCPUTargetInfo::setAuxTarget(const TargetInfo *Aux) {
7371
getTargetOpts() = Aux->getTargetOpts();
7472
resetDataLayout(Aux->getDataLayoutString());
7573
}
74+
75+
// A target may initialise its DataLayoutString and potentially other features
76+
// in `handleTargetFeatures` (as opposed to its constructor), so we can only
77+
// copy the features and query DataLayoutString after that function was called.
78+
bool NativeCPUTargetInfo::handleTargetFeatures(
79+
std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
80+
if (HostTarget) {
81+
if (!HostTarget->handleTargetFeatures(Features, Diags))
82+
return false;
83+
copyAuxTarget(&*HostTarget);
84+
resetDataLayout(HostTarget->getDataLayoutString());
85+
}
86+
return true;
87+
}

clang/lib/Basic/Targets/NativeCPU.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class LLVM_LIBRARY_VISIBILITY NativeCPUTargetInfo final : public TargetInfo {
5656
return TargetInfo::checkCallingConvention(CC);
5757
}
5858

59+
bool handleTargetFeatures(std::vector<std::string> &Features,
60+
DiagnosticsEngine &Diags) override;
61+
5962
protected:
6063
void setAuxTarget(const TargetInfo *Aux) override;
6164

clang/test/CodeGenSYCL/native_cpu_target_features.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
// This is not sensible but check that we do not crash.
66
// RUN: %clang_cc1 -triple native_cpu -aux-triple native_cpu -fsycl-is-device -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,NOX86,NOAVX
77

8+
// Ensures NativeCPU does not cause a compiler assert when querying the host
9+
// target's DataLayoutString, which used to happen on ARM which initializes
10+
// DataLayoutString not in its constructor but afterwards.
11+
// RUN: %clang_cc1 -fsycl-is-device -triple native_cpu -aux-triple aarch64-arm-none-eabi -emit-llvm-bc -o %t %s
12+
813
#include "Inputs/sycl.hpp"
914
using namespace sycl;
1015

0 commit comments

Comments
 (0)