Skip to content

Commit 696a18b

Browse files
authored
Propagate the target to clang-cl when it's used to preprocess C. (#4627)
1 parent 041f9c2 commit 696a18b

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

driver/cpreprocessor.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
#include "driver/cl_options.h"
55
#include "driver/timetrace.h"
66
#include "driver/tool.h"
7+
#include "gen/irstate.h"
78
#include "llvm/Support/FileSystem.h"
89
#include "llvm/Support/Path.h"
910
#include "llvm/Support/Program.h"
11+
#include "llvm/MC/MCSubtargetInfo.h"
12+
#include "llvm/Target/TargetMachine.h"
1013

1114
namespace {
1215
const char *getPathToImportc_h(const Loc &loc) {
@@ -119,6 +122,27 @@ FileName runCPreprocessor(FileName csrcfile, const Loc &loc,
119122
args.push_back("/PD"); // print all macro definitions
120123
args.push_back("/Zc:preprocessor"); // use the new conforming preprocessor
121124
} else {
125+
// propagate the target to the preprocessor
126+
args.push_back("-target");
127+
args.push_back(triple.getTriple());
128+
129+
#if LDC_LLVM_VER >= 1800 // getAllProcessorFeatures was introduced in this version
130+
// propagate all enabled/disabled features to the preprocessor
131+
const auto &subTarget = gTargetMachine->getMCSubtargetInfo();
132+
const auto &featureBits = subTarget->getFeatureBits();
133+
llvm::SmallString<64> featureString;
134+
for (const auto &feature : subTarget->getAllProcessorFeatures()) {
135+
args.push_back("-Xclang");
136+
args.push_back("-target-feature");
137+
args.push_back("-Xclang");
138+
139+
featureString += featureBits.test(feature.Value) ? '+' : '-';
140+
featureString += feature.Key;
141+
args.push_back(featureString.str().str());
142+
featureString.clear();
143+
}
144+
#endif
145+
122146
// print macro definitions (clang-cl doesn't support /PD - use clang's
123147
// -dD)
124148
args.push_back("-Xclang");
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// On Windows, either clang-cl or cl is used to preprocess C.
2+
// If clang-cl is used, LDC's target triple and any target features should be
3+
// passed to clang-cl.
4+
5+
// REQUIRES: Windows && target_X86 && target_AArch64
6+
7+
// RUN: %ldc -mtriple=x86_64-pc-windows-msvc -mcpu=znver1 -v -c %S/inputs/preprocessable.c | FileCheck %s -check-prefix=znver1
8+
// RUN: %ldc -mtriple=x86_64-pc-windows-msvc -mcpu=znver1 -mattr -sse4a -v -c %S/inputs/preprocessable.c | FileCheck %s -check-prefix=znver1-sans-sse4a
9+
// RUN: %ldc -mtriple=x86_64-pc-windows-msvc -mcpu=znver2 -v -c %S/inputs/preprocessable.c | FileCheck %s -check-prefix=znver2
10+
// RUN: %ldc -mtriple=aarch64-pc-windows-msvc -mcpu=apple-a10 -v -c %S/inputs/preprocessable.c | FileCheck %s -check-prefix=apple-a10
11+
// RUN: %ldc -mtriple=aarch64-pc-windows-msvc -mcpu=apple-a11 -v -c %S/inputs/preprocessable.c | FileCheck %s -check-prefix=apple-a11
12+
13+
// znver1: {{\\cl\.exe[[:space:]]|\\clang-cl\.exe[[:space:]].*-target[[:space:]]+x86_64-pc-windows-msvc.*-Xclang[[:space:]]+-target-feature[[:space:]]+-Xclang[[:space:]]+\-clwb.*-Xclang[[:space:]]+-target-feature[[:space:]]+-Xclang[[:space:]]+\+sse4a}}
14+
// znver1-sans-sse4a: {{\\cl\.exe[[:space:]]|\\clang-cl\.exe[[:space:]].*-target[[:space:]]+x86_64-pc-windows-msvc.*-Xclang[[:space:]]+-target-feature[[:space:]]+-Xclang[[:space:]]+\-clwb.*-Xclang[[:space:]]+-target-feature[[:space:]]+-Xclang[[:space:]]+\-sse4a}}
15+
// znver2: {{\\cl\.exe[[:space:]]|\\clang-cl\.exe[[:space:]].*-target[[:space:]]+x86_64-pc-windows-msvc.*-Xclang[[:space:]]+-target-feature[[:space:]]+-Xclang[[:space:]]+\+clwb}}
16+
// apple-a10: {{\\cl\.exe[[:space:]]|\\clang-cl\.exe[[:space:]].*-target[[:space:]]+aarch64-pc-windows-msvc.*-Xclang[[:space:]]+-target-feature[[:space:]]+-Xclang[[:space:]]+\+apple-a10.*-Xclang[[:space:]]+-target-feature[[:space:]]+-Xclang[[:space:]]+\-apple-a11}}
17+
// apple-a11: {{\\cl\.exe[[:space:]]|\\clang-cl\.exe[[:space:]].*-target[[:space:]]+aarch64-pc-windows-msvc.*-Xclang[[:space:]]+-target-feature[[:space:]]+-Xclang[[:space:]]+\-apple-a10.*-Xclang[[:space:]]+-target-feature[[:space:]]+-Xclang[[:space:]]+\+apple-a11}}

tests/driver/inputs/preprocessable.c

Whitespace-only changes.

0 commit comments

Comments
 (0)