Skip to content

Commit f60eed9

Browse files
authored
llvm-reduce: Add target-features-attr reduction (llvm#133887)
Try to reduce individual subtarget features in the "target-features" attribute. This attempts a textual removal of the fields in the string, not a semantic removal. Typically there's a lot of redundant feature spam in the feature list implied by the target-cpu (which I really wish clang would stop emitting). If we could parse these out, we could easily drop the fields without testing anything.
1 parent 25622aa commit f60eed9

File tree

7 files changed

+155
-0
lines changed

7 files changed

+155
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=target-features-attr --test FileCheck --test-arg -enable-var-scope --test-arg --check-prefixes=INTERESTING,CHECK --test-arg %s --test-arg --input-file %s -o %t
2+
; RUN: FileCheck -check-prefixes=RESULT,CHECK %s < %t
3+
4+
; CHECK: @keep_none_from_one() [[$KEEP_NONE_FROM_ONE:#[0-9]+]]
5+
define void @keep_none_from_one() #0 {
6+
ret void
7+
}
8+
9+
; CHECK: @keep_one_from_one() [[$KEEP_ONE_FROM_ONE:#[0-9]+]]
10+
define void @keep_one_from_one() #1 {
11+
ret void
12+
}
13+
14+
; CHECK: @keep_first_from_two() [[$KEEP_FIRST_FROM_TWO:#[0-9]+]]
15+
define void @keep_first_from_two() #2 {
16+
ret void
17+
}
18+
19+
; CHECK: @keep_second_from_two() [[$KEEP_SECOND_FROM_TWO:#[0-9]+]]
20+
define void @keep_second_from_two() #3 {
21+
ret void
22+
}
23+
24+
; CHECK: @keep_all_of_two() [[$KEEP_ALL_OF_TWO:#[0-9]+]]
25+
define void @keep_all_of_two() #4 {
26+
ret void
27+
}
28+
29+
; CHECK: @drop_empty_element() [[$DROP_EMPTY_ELEMENT:#[0-9]+]]
30+
define void @drop_empty_element() #5 {
31+
ret void
32+
}
33+
34+
; CHECK: @keep_second_from_three() [[$KEEP_SECOND_FROM_THREE:#[0-9]+]]
35+
define void @keep_second_from_three() #6 {
36+
ret void
37+
}
38+
39+
; RESULT: define void @no_target_features() {
40+
define void @no_target_features() {
41+
ret void
42+
}
43+
44+
; IR verifier should probably reject this
45+
; RESULT: define void @no_target_features_value() {
46+
define void @no_target_features_value() #7 {
47+
ret void
48+
}
49+
50+
attributes #0 = { "target-features"="+foo" "unique-attr-0" }
51+
attributes #1 = { "target-features"="+foo" "unique-attr-1" }
52+
attributes #2 = { "target-features"="+first,+second" "unique-attr-2" }
53+
attributes #3 = { "target-features"="+first,+second" "unique-attr-3" }
54+
attributes #4 = { "target-features"="+first,+second" "unique-attr-4" }
55+
attributes #5 = { "target-features"="+dead,,+beef" "unique-attr-5" }
56+
attributes #6 = { "target-features"="+a,+b,+c" "unique-attr-6" }
57+
attributes #7 = { "target-features" }
58+
59+
; INTERESTING-DAG: [[$KEEP_ONE_FROM_ONE]] = { "target-features"="+foo"
60+
; INTERESTING-DAG: [[$KEEP_FIRST_FROM_TWO]] = { "target-features"="{{.*}}+first
61+
; INTERESTING-DAG: [[$KEEP_SECOND_FROM_TWO]] = { "target-features"="{{.*}}+second
62+
; INTERESTING-DAG: [[$KEEP_ALL_OF_TWO]] = { "target-features"="{{.*}}+first,+second
63+
; INTERESTING-DAG: [[$DROP_EMPTY_ELEMENT]] = { "target-features"="{{.*}}+dead{{.*}}+beef
64+
; INTERESTING-DAG: [[$KEEP_SECOND_FROM_THREE]] = { "target-features"="{{.*}}+b
65+
66+
67+
; RESULT-DAG: attributes [[$KEEP_NONE_FROM_ONE]] = { "unique-attr-0" }
68+
; RESULT-DAG: [[$KEEP_FIRST_FROM_TWO]] = { "target-features"="+first" "unique-attr-2" }
69+
; RESULT-DAG: [[$KEEP_SECOND_FROM_TWO]] = { "target-features"="+second" "unique-attr-3" }
70+
; RESULT-DAG: [[$KEEP_ALL_OF_TWO]] = { "target-features"="+first,+second" "unique-attr-4" }
71+
; RESULT-DAG: [[$DROP_EMPTY_ELEMENT]] = { "target-features"="+dead,+beef" "unique-attr-5" }
72+
; RESULT-DAG: [[$KEEP_SECOND_FROM_THREE]] = { "target-features"="+b" "unique-attr-6" }

llvm/tools/llvm-reduce/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ add_llvm_tool(llvm-reduce
5858
deltas/ReduceRegisterMasks.cpp
5959
deltas/ReduceRegisterDefs.cpp
6060
deltas/ReduceRegisterUses.cpp
61+
deltas/ReduceTargetFeaturesAttr.cpp
6162
deltas/ReduceUsingSimplifyCFG.cpp
6263
deltas/RunIRPasses.cpp
6364
deltas/SimplifyInstructions.cpp

llvm/tools/llvm-reduce/DeltaManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "deltas/ReduceRegisterMasks.h"
4646
#include "deltas/ReduceRegisterUses.h"
4747
#include "deltas/ReduceSpecialGlobals.h"
48+
#include "deltas/ReduceTargetFeaturesAttr.h"
4849
#include "deltas/ReduceUsingSimplifyCFG.h"
4950
#include "deltas/ReduceVirtualRegisters.h"
5051
#include "deltas/RunIRPasses.h"

llvm/tools/llvm-reduce/DeltaPasses.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ DELTA_PASS_IR("operands-to-args", reduceOperandsToArgsDeltaPass, "Converting ope
4545
DELTA_PASS_IR("operands-skip", reduceOperandsSkipDeltaPass, "Reducing operands by skipping over instructions")
4646
DELTA_PASS_IR("operand-bundles", reduceOperandBundesDeltaPass, "Reducing Operand Bundles")
4747
DELTA_PASS_IR("attributes", reduceAttributesDeltaPass, "Reducing Attributes")
48+
DELTA_PASS_IR("target-features-attr", reduceTargetFeaturesAttrDeltaPass, "Reducing target-features")
4849
DELTA_PASS_IR("module-data", reduceModuleDataDeltaPass, "Reducing Module Data")
4950
DELTA_PASS_IR("opcodes", reduceOpcodesDeltaPass, "Reducing Opcodes")
5051
DELTA_PASS_IR("volatile", reduceVolatileInstructionsDeltaPass, "Reducing Volatile Instructions")
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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("target-features");
31+
if (!TargetFeaturesAttr.isValid())
32+
continue;
33+
34+
StringRef TargetFeatures = TargetFeaturesAttr.getValueAsString();
35+
TargetFeatures.split(SplitFeatures, ',', /*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("target-features");
50+
else
51+
F.addFnAttr("target-features", NewValueString);
52+
53+
SplitFeatures.clear();
54+
NewValueString.clear();
55+
}
56+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===- ReduceTargetFeaturesAttr.h - Specialized Delta Pass ------*- C++ -*-===//
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+
// This file implements a function which calls the Generic Delta pass in order
10+
// to reduce uninteresting attributes.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCETARGETFEATURESATTR_H
15+
#define LLVM_TOOLS_LLVM_REDUCE_DELTAS_REDUCETARGETFEATURESATTR_H
16+
17+
#include "Delta.h"
18+
19+
namespace llvm {
20+
void reduceTargetFeaturesAttrDeltaPass(Oracle &O, ReducerWorkItem &WorkItem);
21+
} // namespace llvm
22+
23+
#endif

llvm/utils/gn/secondary/llvm/tools/llvm-reduce/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ executable("llvm-reduce") {
4747
"deltas/ReduceRegisterDefs.cpp",
4848
"deltas/ReduceRegisterMasks.cpp",
4949
"deltas/ReduceRegisterUses.cpp",
50+
"deltas/ReduceTargetFeaturesAttr.cpp",
5051
"deltas/ReduceSpecialGlobals.cpp",
5152
"deltas/ReduceUsingSimplifyCFG.cpp",
5253
"deltas/ReduceVirtualRegisters.cpp",

0 commit comments

Comments
 (0)