Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions clang/lib/Basic/Targets/NativeCPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ static const LangASMap NativeCPUASMap = {
20, // wasm_funcref
};

NativeCPUTargetInfo::NativeCPUTargetInfo(const llvm::Triple &,
NativeCPUTargetInfo::NativeCPUTargetInfo(const llvm::Triple &Triple,
const TargetOptions &Opts)
: TargetInfo(llvm::Triple()) {
: TargetInfo(Triple) {
AddrSpaceMap = &NativeCPUASMap;
UseAddrSpaceMapMangling = true;
HasLegalHalfType = true;
Expand All @@ -54,11 +54,11 @@ NativeCPUTargetInfo::NativeCPUTargetInfo(const llvm::Triple &,
// Take the default target triple if no other host triple is specified so
// that system headers work.
if (Opts.HostTriple.empty())
return llvm::sys::getDefaultTargetTriple();
return llvm::Triple(llvm::sys::getDefaultTargetTriple());

return Opts.HostTriple;
return llvm::Triple(Opts.HostTriple);
}());
if (HostTriple.getArch() != llvm::Triple::UnknownArch) {
if (!HostTriple.isNativeCPU()) {
HostTarget = AllocateTarget(HostTriple, Opts);
copyAuxTarget(&*HostTarget);
}
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ add_clang_library(clangCodeGen
Targets/MSP430.cpp
Targets/Mips.cpp
Targets/NVPTX.cpp
Targets/NativeCPU.cpp
Targets/PNaCl.cpp
Targets/PPC.cpp
Targets/RISCV.cpp
Expand Down
42 changes: 39 additions & 3 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ static bool SYCLCUDAIsSYCLDevice(const clang::LangOptions &LangOpts) {
}

static std::unique_ptr<TargetCodeGenInfo>
createTargetCodeGenInfo(CodeGenModule &CGM) {
const TargetInfo &Target = CGM.getTarget();
const llvm::Triple &Triple = Target.getTriple();
createTargetCodeGenInfo(CodeGenModule &CGM, const TargetInfo &Target,
const llvm::Triple &Triple) {
const CodeGenOptions &CodeGenOpts = CGM.getCodeGenOpts();

switch (Triple.getArch()) {
Expand Down Expand Up @@ -335,9 +334,46 @@ createTargetCodeGenInfo(CodeGenModule &CGM) {
return createLoongArchTargetCodeGenInfo(
CGM, Target.getPointerWidth(LangAS::Default), ABIFRLen);
}

case llvm::Triple::native_cpu: {
std::unique_ptr<TargetCodeGenInfo> HostTargetCodeGenInfo;
const auto &TargetOpts = Target.getTargetOpts();

// Normally we will be compiling in SYCL mode, in which the options have
// been overwritten with the host options, we get the host triple in
// TargetOpts.Triple, and TargetOpts.HostTriple is meaningless. However,
// during the libclc build, this is not the case and we need to figure it
// out from TargetOpts.HostTriple.
llvm::Triple HostTriple(TargetOpts.Triple);
if (HostTriple.isNativeCPU()) {
// This should be kept in sync with NativeCPUTargetInfo's constructor.
// Ideally we would cast to NativeCPUTargetInfo and just access the host
// target directly but ASTContext does not guarantee that it is a
// NativeCPUTargetInfo.
HostTriple = [&] {
if (TargetOpts.HostTriple.empty())
return llvm::Triple(llvm::sys::getDefaultTargetTriple());

return llvm::Triple(TargetOpts.HostTriple);
}();
}
if (!HostTriple.isNativeCPU()) {
HostTargetCodeGenInfo = createTargetCodeGenInfo(CGM, Target, HostTriple);
}

return createNativeCPUTargetCodeGenInfo(CGM,
std::move(HostTargetCodeGenInfo));
}
}
}

static std::unique_ptr<TargetCodeGenInfo>
createTargetCodeGenInfo(CodeGenModule &CGM) {
const TargetInfo &Target = CGM.getTarget();
const llvm::Triple &Triple = Target.getTriple();
return createTargetCodeGenInfo(CGM, Target, Triple);
}

const TargetCodeGenInfo &CodeGenModule::getTargetCodeGenInfo() {
if (!TheTargetCodeGenInfo)
TheTargetCodeGenInfo = createTargetCodeGenInfo(*this);
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/CodeGen/TargetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ createMSP430TargetCodeGenInfo(CodeGenModule &CGM);
std::unique_ptr<TargetCodeGenInfo>
createNVPTXTargetCodeGenInfo(CodeGenModule &CGM);

std::unique_ptr<TargetCodeGenInfo>
createNativeCPUTargetCodeGenInfo(CodeGenModule &CGM,
std::unique_ptr<TargetCodeGenInfo>);

std::unique_ptr<TargetCodeGenInfo>
createPNaClTargetCodeGenInfo(CodeGenModule &CGM);

Expand Down
45 changes: 45 additions & 0 deletions clang/lib/CodeGen/Targets/NativeCPU.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===- NativeCPU.cpp ------------------------------------------------------===//
//
// 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 "ABIInfoImpl.h"
#include "TargetInfo.h"

using namespace clang;
using namespace clang::CodeGen;

namespace {
class NativeCPUABIInfo : public DefaultABIInfo {
private:
const ABIInfo *HostABIInfo;

public:
NativeCPUABIInfo(CodeGen::CodeGenTypes &CGT, const ABIInfo *HostABIInfo)
: DefaultABIInfo(CGT), HostABIInfo(HostABIInfo) {}
};

class NativeCPUTargetCodeGenInfo : public TargetCodeGenInfo {
private:
std::unique_ptr<TargetCodeGenInfo> HostTargetCodeGenInfo;

public:
NativeCPUTargetCodeGenInfo(
CodeGen::CodeGenTypes &CGT,
std::unique_ptr<TargetCodeGenInfo> HostTargetCodeGenInfo)
: TargetCodeGenInfo(std::make_unique<NativeCPUABIInfo>(
CGT, HostTargetCodeGenInfo ? &HostTargetCodeGenInfo->getABIInfo()
: nullptr)),
HostTargetCodeGenInfo(std::move(HostTargetCodeGenInfo)) {}
};
} // namespace

std::unique_ptr<TargetCodeGenInfo> CodeGen::createNativeCPUTargetCodeGenInfo(
CodeGenModule &CGM,
std::unique_ptr<TargetCodeGenInfo> HostTargetCodeGenInfo) {
return std::make_unique<NativeCPUTargetCodeGenInfo>(
CGM.getTypes(), std::move(HostTargetCodeGenInfo));
}
3 changes: 3 additions & 0 deletions clang/test/CodeGenSYCL/native_cpu_target_features.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// RUN: %clang_cc1 -triple native_cpu -aux-triple x86_64-unknown-linux-gnu -fsycl-is-device -emit-llvm -fsycl-is-native-cpu -o - %s | FileCheck %s --check-prefixes=CHECK,NOAVX
// RUN: %clang_cc1 -triple native_cpu -aux-triple x86_64-unknown-linux-gnu -aux-target-cpu skylake -fsycl-is-device -emit-llvm -fsycl-is-native-cpu -o - %s | FileCheck %s --check-prefixes=CHECK,AVX
// RUN: %clang_cc1 -triple native_cpu -aux-triple x86_64-unknown-linux-gnu -aux-target-feature +avx -fsycl-is-device -emit-llvm -fsycl-is-native-cpu -o - %s | FileCheck %s --check-prefixes=CHECK,AVX
//
// This is not sensible but check that we do not crash.
// RUN: %clang_cc1 -triple native_cpu -aux-triple native_cpu -fsycl-is-device -emit-llvm -fsycl-is-native-cpu -o - %s | FileCheck %s --check-prefixes=CHECK,NOAVX

#include "Inputs/sycl.hpp"
using namespace sycl;
Expand Down
6 changes: 2 additions & 4 deletions libclc/utils/prepare-builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,9 @@ int main(int argc, char **argv) {
//
// NativeCPU uses the same builtins for multiple host targets and should
// likewise not have features that limit the builtins to any particular
// target. It does not record any target triple so as to not confuse opt.
// TODO If this gets upstreamed into LLVM, it should be recognized in
// llvm::Triple.
// target.
if (M->getTargetTriple().str().find("amdgcn") != std::string::npos ||
M->getTargetTriple().str() == "") {
M->getTargetTriple().isNativeCPU()) {
AttributeMask AM;
AM.addAttribute("target-features");
AM.addAttribute("target-cpu");
Expand Down