From: Eric Christopher Date: Wed, 1 Jul 2015 00:08:29 +0000 (+0000) Subject: Fix a TODO dealing with canonicalizing attributes on functions by X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=73c84e326a17c9547ef49c0393f5067d940459d7;p=clang Fix a TODO dealing with canonicalizing attributes on functions by using a string map to canonicalize. Fix up a couple of testcases that needed changing since we are no longer simply appending features to the list, but all of their mask dependencies as well. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@241129 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGCall.cpp b/lib/CodeGen/CGCall.cpp index 58ef171df0..245b8274a9 100644 --- a/lib/CodeGen/CGCall.cpp +++ b/lib/CodeGen/CGCall.cpp @@ -1493,12 +1493,16 @@ void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, // 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 or // figure out non-explicit dependencies. - std::vector Features(getTarget().getTargetOpts().Features); + // Canonicalize the existing features in a new feature map. + // TODO: Migrate the existing backends to keep the map around rather than + // the vector. + llvm::StringMap FeatureMap; + for (auto F : getTarget().getTargetOpts().Features) { + const char *Name = F.c_str(); + bool Enabled = Name[0] == '+'; + getTarget().setFeatureEnabled(FeatureMap, Name + 1, Enabled); + } - // TODO: The target attribute complicates this further by allowing multiple - // additional features to be tacked on to the feature string for a - // particular function. For now we simply append to the set of features and - // let backend resolution fix them up. const FunctionDecl *FD = dyn_cast_or_null(TargetDecl); if (FD) { if (const TargetAttr *TD = FD->getAttr()) { @@ -1507,7 +1511,7 @@ void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, FeaturesStr.split(AttrFeatures, ","); // Grab the various features and prepend a "+" to turn on the feature to - // the backend and add them to our existing set of Features. + // the backend and add them to our existing set of features. for (auto &Feature : AttrFeatures) { // While we're here iterating check for a different target cpu. if (Feature.startswith("arch=")) @@ -1521,13 +1525,21 @@ void CodeGenModule::ConstructAttributeList(const CGFunctionInfo &FI, // attributes on the function. ; else if (Feature.startswith("mno-")) - Features.push_back("-" + Feature.split("-").second.str()); + getTarget().setFeatureEnabled(FeatureMap, Feature.split("-").second, + false); else - Features.push_back("+" + Feature.str()); - } + getTarget().setFeatureEnabled(FeatureMap, Feature, true); + } } } + // Produce the canonical string for this set of features. + std::vector Features; + for (llvm::StringMap::const_iterator it = FeatureMap.begin(), + ie = FeatureMap.end(); + it != ie; ++it) + Features.push_back((it->second ? "+" : "-") + it->first().str()); + // Now add the target-cpu and target-features to the function. if (TargetCPU != "") FuncAttrs.addAttribute("target-cpu", TargetCPU); diff --git a/test/CodeGen/attr-target.c b/test/CodeGen/attr-target.c index 8c0f335f85..a42705a478 100644 --- a/test/CodeGen/attr-target.c +++ b/test/CodeGen/attr-target.c @@ -21,5 +21,5 @@ int bar(int a) { return baz(a) + foo(a); } // CHECK: echidna{{.*}} #2 // CHECK: bar{{.*}} #0 // CHECK: #0 = {{.*}}"target-cpu"="x86-64" "target-features"="+sse,+sse2" -// CHECK: #1 = {{.*}}"target-cpu"="ivybridge" "target-features"="+sse,+sse2,+avx,+sse4.2" -// CHECK: #2 = {{.*}}"target-cpu"="x86-64" "target-features"="+sse,+sse2,-sse2" +// CHECK: #1 = {{.*}}"target-cpu"="ivybridge" "target-features"="+sse4.2,+ssse3,+sse3,+sse,+sse2,+sse4.1,+avx" +// CHECK: #2 = {{.*}}"target-cpu"="x86-64" "target-features"="-sse4a,-sse3,-avx2,-avx512bw,-avx512er,-avx512dq,-avx512pf,-fma4,-avx512vl,+sse,-pclmul,-avx512cd,-avx,-f16c,-ssse3,-avx512f,-fma,-xop,-aes,-sha,-sse2,-sse4.1,-sse4.2" diff --git a/test/CodeGen/function-target-features.c b/test/CodeGen/function-target-features.c index 351c7f102b..133e89cbbe 100644 --- a/test/CodeGen/function-target-features.c +++ b/test/CodeGen/function-target-features.c @@ -17,7 +17,7 @@ void foo() {} // AVX-FEATURE: "target-features"{{.*}}+avx // AVX-NO-CPU-NOT: target-cpu -// TWO-AVX: "target-features"={{.*}}+avx512er{{.*}}+avx512f +// TWO-AVX: "target-features"={{.*}}+avx512f{{.*}}+avx512er // CORE-CPU: "target-cpu"="corei7" // CORE-CPU-AND-FEATURES: "target-cpu"="corei7" "target-features"={{.*}}+avx // X86-64-CPU: "target-cpu"="x86-64"