| 1 | //===- ReduceTargetFeaturesAttr.cpp - Specialized Delta Pass --------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Attempt to remove individual elements of the "target-features" attribute on |
| 10 | // functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ReduceTargetFeaturesAttr.h" |
| 15 | |
| 16 | #include "llvm/ADT/SmallString.h" |
| 17 | #include "llvm/ADT/StringExtras.h" |
| 18 | #include "llvm/IR/Function.h" |
| 19 | |
| 20 | // TODO: We could maybe do better if we did a semantic parse of the attributes |
| 21 | // through MCSubtargetInfo. Features can be flipped on and off in the string, |
| 22 | // some are implied by target-cpu and can't be meaningfully re-added. |
| 23 | void llvm::reduceTargetFeaturesAttrDeltaPass(Oracle &O, |
| 24 | ReducerWorkItem &WorkItem) { |
| 25 | Module &M = WorkItem.getModule(); |
| 26 | SmallString<256> NewValueString; |
| 27 | SmallVector<StringRef, 32> SplitFeatures; |
| 28 | |
| 29 | for (Function &F : M) { |
| 30 | Attribute TargetFeaturesAttr = F.getFnAttribute(Kind: "target-features" ); |
| 31 | if (!TargetFeaturesAttr.isValid()) |
| 32 | continue; |
| 33 | |
| 34 | StringRef TargetFeatures = TargetFeaturesAttr.getValueAsString(); |
| 35 | TargetFeatures.split(A&: SplitFeatures, Separator: ',', /*MaxSplit=*/-1, |
| 36 | /*KeepEmpty=*/false); |
| 37 | |
| 38 | ListSeparator LS("," ); |
| 39 | |
| 40 | { |
| 41 | raw_svector_ostream OS(NewValueString); |
| 42 | for (StringRef Feature : SplitFeatures) { |
| 43 | if (O.shouldKeep()) |
| 44 | OS << LS << Feature; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | if (NewValueString.empty()) |
| 49 | F.removeFnAttr(Kind: "target-features" ); |
| 50 | else |
| 51 | F.addFnAttr(Kind: "target-features" , Val: NewValueString); |
| 52 | |
| 53 | SplitFeatures.clear(); |
| 54 | NewValueString.clear(); |
| 55 | } |
| 56 | } |
| 57 | |