1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
//===- ReduceAttributes.cpp - Specialized Delta Pass ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements a function which calls the Generic Delta pass in order
// to reduce uninteresting attributes.
//
//===----------------------------------------------------------------------===//
#include "ReduceAttributes.h"
#include "Delta.h"
#include "TestRunner.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/InstVisitor.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <iterator>
#include <utility>
namespace llvm {
class LLVMContext;
} // namespace llvm
using namespace llvm;
namespace {
/// Given ChunksToKeep, produce a map of global variables/functions/calls
/// and indexes of attributes to be preserved for each of them.
class AttributeRemapper : public InstVisitor<AttributeRemapper> {
Oracle &O;
LLVMContext &Context;
public:
AttributeRemapper(Oracle &O, LLVMContext &C) : O(O), Context(C) {}
void visitModule(Module &M) {
for (GlobalVariable &GV : M.globals())
visitGlobalVariable(GV);
}
void visitGlobalVariable(GlobalVariable &GV) {
// Global variables only have one attribute set.
AttributeSet AS = GV.getAttributes();
if (AS.hasAttributes()) {
AttrBuilder AttrsToPreserve(Context);
visitAttributeSet(AS, AttrsToPreserve);
GV.setAttributes(AttributeSet::get(Context, AttrsToPreserve));
}
}
void visitFunction(Function &F) {
// We can neither add nor remove attributes from intrinsics.
if (F.getIntrinsicID() == Intrinsic::not_intrinsic)
F.setAttributes(visitAttributeList(F.getAttributes()));
}
void visitCallBase(CallBase &CB) {
CB.setAttributes(visitAttributeList(CB.getAttributes()));
}
AttributeSet visitAttributeIndex(AttributeList AL, unsigned Index) {
AttrBuilder AttributesToPreserve(Context);
visitAttributeSet(AL.getAttributes(Index), AttributesToPreserve);
if (AttributesToPreserve.attrs().empty())
return {};
return AttributeSet::get(Context, AttributesToPreserve);
}
AttributeList visitAttributeList(AttributeList AL) {
SmallVector<std::pair<unsigned, AttributeSet>> NewAttrList;
NewAttrList.reserve(AL.getNumAttrSets());
for (unsigned SetIdx : AL.indexes()) {
if (SetIdx == AttributeList::FunctionIndex)
continue;
AttributeSet AttrSet = visitAttributeIndex(AL, SetIdx);
if (AttrSet.hasAttributes())
NewAttrList.emplace_back(SetIdx, AttrSet);
}
// FIXME: It's ridiculous that indexes() doesn't give us the correct order
// for contructing a new AttributeList. Special case the function index so
// we don't have to sort.
AttributeSet FnAttrSet =
visitAttributeIndex(AL, AttributeList::FunctionIndex);
if (FnAttrSet.hasAttributes())
NewAttrList.emplace_back(AttributeList::FunctionIndex, FnAttrSet);
return AttributeList::get(Context, NewAttrList);
}
void visitAttributeSet(const AttributeSet &AS, AttrBuilder &AttrsToPreserve) {
// Optnone requires noinline, so removing noinline requires removing the
// pair.
Attribute NoInline = AS.getAttribute(Attribute::NoInline);
bool RemoveNoInline = false;
if (NoInline.isValid()) {
RemoveNoInline = !O.shouldKeep();
if (!RemoveNoInline)
AttrsToPreserve.addAttribute(NoInline);
}
for (Attribute A : AS) {
if (A.isEnumAttribute()) {
Attribute::AttrKind Kind = A.getKindAsEnum();
if (Kind == Attribute::NoInline)
continue;
if (RemoveNoInline && Kind == Attribute::OptimizeNone)
continue;
// TODO: Could only remove this if there are no constrained calls in the
// function.
if (Kind == Attribute::StrictFP) {
AttrsToPreserve.addAttribute(A);
continue;
}
}
if (O.shouldKeep())
AttrsToPreserve.addAttribute(A);
}
}
};
} // namespace
/// Removes out-of-chunk attributes from module.
static void extractAttributesFromModule(Oracle &O, ReducerWorkItem &WorkItem) {
AttributeRemapper R(O, WorkItem.getContext());
R.visit(WorkItem.getModule());
}
void llvm::reduceAttributesDeltaPass(TestRunner &Test) {
runDeltaPass(Test, extractAttributesFromModule, "Reducing Attributes");
}
|