r246706 - Migrate the target attribute parsing code to returning an instance

Eric Christopher via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 2 13:40:12 PDT 2015


Author: echristo
Date: Wed Sep  2 15:40:12 2015
New Revision: 246706

URL: http://llvm.org/viewvc/llvm-project?rev=246706&view=rev
Log:
Migrate the target attribute parsing code to returning an instance
every time it's called rather than attempting to cache the result.
It's unlikely to be called frequently and the overhead of using
it in the first place is already factored out.

Modified:
    cfe/trunk/include/clang/Basic/Attr.td
    cfe/trunk/lib/CodeGen/CGCall.cpp

Modified: cfe/trunk/include/clang/Basic/Attr.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Attr.td?rev=246706&r1=246705&r2=246706&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Attr.td (original)
+++ cfe/trunk/include/clang/Basic/Attr.td Wed Sep  2 15:40:12 2015
@@ -1321,20 +1321,9 @@ def Target : InheritableAttr {
   let Subjects = SubjectList<[Function], ErrorDiag>;
   let Documentation = [TargetDocs];
   let AdditionalMembers = [{
-    StringRef CPU;
-    std::vector<std::string> Features;
-    bool Parsed = false;
-    StringRef getCPU() {
-      if (!Parsed)
-        parse();
-      return CPU;
-    }
-    std::vector<std::string> &getFeatures() {
-      if (!Parsed)
-        parse();
-      return Features;
-    }
-    void parse() {
+    typedef std::pair<std::vector<std::string>, StringRef> ParsedTargetAttr;
+    ParsedTargetAttr parse() const {
+      ParsedTargetAttr Ret;
       SmallVector<StringRef, 1> AttrFeatures;
       getFeaturesStr().split(AttrFeatures, ",");
 
@@ -1354,13 +1343,13 @@ def Target : InheritableAttr {
 
         // While we're here iterating check for a different target cpu.
         if (Feature.startswith("arch="))
-          CPU = Feature.split("=").second.trim();
+          Ret.second = Feature.split("=").second.trim();
         else if (Feature.startswith("no-"))
-          Features.push_back("-" + Feature.split("-").second.str());
+          Ret.first.push_back("-" + Feature.split("-").second.str());
         else
-          Features.push_back("+" + Feature.str());
+          Ret.first.push_back("+" + Feature.str());
       }
-      Parsed = true;
+      return Ret;
     }
   }];
 }

Modified: cfe/trunk/lib/CodeGen/CGCall.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCall.cpp?rev=246706&r1=246705&r2=246706&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGCall.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCall.cpp Wed Sep  2 15:40:12 2015
@@ -1499,24 +1499,24 @@ void CodeGenModule::ConstructAttributeLi
     const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(TargetDecl);
     if (FD && FD->hasAttr<TargetAttr>()) {
       llvm::StringMap<bool> FeatureMap;
-      auto *TD = FD->getAttr<TargetAttr>();
+      const auto *TD = FD->getAttr<TargetAttr>();
+      TargetAttr::ParsedTargetAttr ParsedAttr = TD->parse();
 
-      // Make a copy of the features as passed on the command line.
-      std::vector<std::string> FnFeatures =
-          getTarget().getTargetOpts().FeaturesAsWritten;
+      // Make a copy of the features as passed on the command line into the
+      // beginning of the additional features from the function to override.
+      ParsedAttr.first.insert(
+          ParsedAttr.first.begin(),
+          getTarget().getTargetOpts().FeaturesAsWritten.begin(),
+          getTarget().getTargetOpts().FeaturesAsWritten.end());
 
-      std::vector<std::string> &AttrFeatures = TD->getFeatures();
-      std::copy(AttrFeatures.begin(), AttrFeatures.end(),
-                std::back_inserter(FnFeatures));
-
-      if (TD->getCPU() != "")
-	TargetCPU = TD->getCPU();
+      if (ParsedAttr.second != "")
+	TargetCPU = ParsedAttr.second;
 
       // Now populate the feature map, first with the TargetCPU which is either
       // the default or a new one from the target attribute string. Then we'll
       // use the passed in features (FeaturesAsWritten) along with the new ones
       // from the attribute.
-      getTarget().initFeatureMap(FeatureMap, Diags, TargetCPU, FnFeatures);
+      getTarget().initFeatureMap(FeatureMap, Diags, TargetCPU, ParsedAttr.first);
 
       // Produce the canonical string for this set of features.
       std::vector<std::string> Features;




More information about the cfe-commits mailing list