From 2bd1fba7c2f7556bfe251ca422932697cecf48f8 Mon Sep 17 00:00:00 2001 From: Eric Christopher Date: Wed, 25 Mar 2015 23:14:47 +0000 Subject: [PATCH] Reapply r232888 after applying a fix for -msse4 code generation. As a note, any target that uses fake target features via command line options will have similar problems. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@233227 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGCall.cpp | 21 +++++++++++++++++++++ test/CodeGen/function-target-features.c | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 test/CodeGen/function-target-features.c diff --git a/lib/CodeGen/CGCall.cpp b/lib/CodeGen/CGCall.cpp index 3cb72bd151..d39776191f 100644 --- a/lib/CodeGen/CGCall.cpp +++ b/lib/CodeGen/CGCall.cpp @@ -31,6 +31,7 @@ #include "llvm/IR/InlineAsm.h" #include "llvm/IR/Intrinsics.h" #include "llvm/Transforms/Utils/Local.h" +#include using namespace clang; using namespace CodeGen; @@ -1475,6 +1476,26 @@ void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, if (!CodeGenOpts.StackRealignment) FuncAttrs.addAttribute("no-realign-stack"); + + // Add target-cpu and target-features work if they differ from the defaults. + std::string &CPU = getTarget().getTargetOpts().CPU; + if (CPU != "" && CPU != getTarget().getTriple().getArchName()) + FuncAttrs.addAttribute("target-cpu", getTarget().getTargetOpts().CPU); + + // TODO: FeaturesAsWritten gets us the features on the command line, + // for canonicalization purposes we might want to avoid putting features + // in the target-features set if we know it'll be one of the default + // features in the backend, e.g. corei7-avx and +avx. + std::vector &Features = + getTarget().getTargetOpts().FeaturesAsWritten; + if (!Features.empty()) { + std::stringstream S; + std::copy(Features.begin(), Features.end(), + std::ostream_iterator(S, ",")); + // The drop_back gets rid of the trailing space. + FuncAttrs.addAttribute("target-features", + StringRef(S.str()).drop_back(1)); + } } ClangToLLVMArgMapping IRFunctionArgs(getContext(), FI); diff --git a/test/CodeGen/function-target-features.c b/test/CodeGen/function-target-features.c new file mode 100644 index 0000000000..5665b1f841 --- /dev/null +++ b/test/CodeGen/function-target-features.c @@ -0,0 +1,21 @@ +// This test verifies that we produce target-cpu and target-features attributes +// on functions when they're different from the standard cpu and have written +// features. + +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s -target-feature +avx | FileCheck %s -check-prefix=AVX-FEATURE +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s -target-feature +avx | FileCheck %s -check-prefix=AVX-NO-CPU +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s -target-feature +avx512f -target-feature +avx512er | FileCheck %s -check-prefix=TWO-AVX +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s -target-cpu corei7 | FileCheck %s -check-prefix=CORE-CPU +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s -target-cpu corei7 -target-feature +avx | FileCheck %s -check-prefix=CORE-CPU-AND-FEATURES +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s -target-cpu x86-64 | FileCheck %s -check-prefix=X86-64-CPU-NOT +// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s -target-cpu corei7-avx -target-feature -avx | FileCheck %s -check-prefix=AVX-MINUS-FEATURE + +void foo() {} + +// AVX-FEATURE: "target-features"="+avx" +// AVX-NO-CPU-NOT: target-cpu +// TWO-AVX: "target-features"="+avx512f,+avx512er" +// CORE-CPU: "target-cpu"="corei7" +// CORE-CPU-AND-FEATURES: "target-cpu"="corei7" "target-features"="+avx" +// X86-64-CPU-NOT: "target-cpu" +// AVX-MINUS-FEATURE: "target-features"="-avx" -- 2.40.0