2021-04-01 20:38:39 -07:00
|
|
|
//===- ReduceAttributes.cpp - Specialized Delta Pass ----------------------===//
|
[llvm-reduce] Reducing attributes
Summary:
This handles all three places where attributes could currently be - `GlobalVariable`, `Function` and `CallBase`.
For last two, it correctly handles all three possible attribute locations (return value, arguments and function itself)
There was a previous attempt at it D73853,
which was committed in rGfc62b36a000681c01e993242b583c5ec4ab48a3c,
but then reverted all the way back in rGb12176d2aafa0ccb2585aa218fc3b454ba84f2a9
due to some (osx?) test failures.
Reviewers: nickdesaulniers, dblaikie, diegotf, george.burgess.iv, jdoerfert, Tyker, arsenm
Reviewed By: nickdesaulniers
Subscribers: wdng, MaskRay, arsenm, llvm-commits, mgorny
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D83351
2020-07-09 23:06:59 +03:00
|
|
|
//
|
|
|
|
// 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/DenseMap.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/Sequence.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/iterator_range.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>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class LLVMContext;
|
|
|
|
} // namespace llvm
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2023-01-02 10:55:59 -05:00
|
|
|
using AttrPtrVecTy = std::vector<Attribute>;
|
[llvm-reduce] Reducing attributes
Summary:
This handles all three places where attributes could currently be - `GlobalVariable`, `Function` and `CallBase`.
For last two, it correctly handles all three possible attribute locations (return value, arguments and function itself)
There was a previous attempt at it D73853,
which was committed in rGfc62b36a000681c01e993242b583c5ec4ab48a3c,
but then reverted all the way back in rGb12176d2aafa0ccb2585aa218fc3b454ba84f2a9
due to some (osx?) test failures.
Reviewers: nickdesaulniers, dblaikie, diegotf, george.burgess.iv, jdoerfert, Tyker, arsenm
Reviewed By: nickdesaulniers
Subscribers: wdng, MaskRay, arsenm, llvm-commits, mgorny
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D83351
2020-07-09 23:06:59 +03:00
|
|
|
using AttrPtrIdxVecVecTy = std::pair<unsigned, AttrPtrVecTy>;
|
|
|
|
using AttrPtrVecVecTy = SmallVector<AttrPtrIdxVecVecTy, 3>;
|
|
|
|
|
|
|
|
/// 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> {
|
2021-10-05 00:05:37 -07:00
|
|
|
Oracle &O;
|
[llvm-reduce] Reducing attributes
Summary:
This handles all three places where attributes could currently be - `GlobalVariable`, `Function` and `CallBase`.
For last two, it correctly handles all three possible attribute locations (return value, arguments and function itself)
There was a previous attempt at it D73853,
which was committed in rGfc62b36a000681c01e993242b583c5ec4ab48a3c,
but then reverted all the way back in rGb12176d2aafa0ccb2585aa218fc3b454ba84f2a9
due to some (osx?) test failures.
Reviewers: nickdesaulniers, dblaikie, diegotf, george.burgess.iv, jdoerfert, Tyker, arsenm
Reviewed By: nickdesaulniers
Subscribers: wdng, MaskRay, arsenm, llvm-commits, mgorny
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D83351
2020-07-09 23:06:59 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
DenseMap<GlobalVariable *, AttrPtrVecTy> GlobalVariablesToRefine;
|
|
|
|
DenseMap<Function *, AttrPtrVecVecTy> FunctionsToRefine;
|
|
|
|
DenseMap<CallBase *, AttrPtrVecVecTy> CallsToRefine;
|
|
|
|
|
2021-10-05 00:05:37 -07:00
|
|
|
explicit AttributeRemapper(Oracle &O) : O(O) {}
|
[llvm-reduce] Reducing attributes
Summary:
This handles all three places where attributes could currently be - `GlobalVariable`, `Function` and `CallBase`.
For last two, it correctly handles all three possible attribute locations (return value, arguments and function itself)
There was a previous attempt at it D73853,
which was committed in rGfc62b36a000681c01e993242b583c5ec4ab48a3c,
but then reverted all the way back in rGb12176d2aafa0ccb2585aa218fc3b454ba84f2a9
due to some (osx?) test failures.
Reviewers: nickdesaulniers, dblaikie, diegotf, george.burgess.iv, jdoerfert, Tyker, arsenm
Reviewed By: nickdesaulniers
Subscribers: wdng, MaskRay, arsenm, llvm-commits, mgorny
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D83351
2020-07-09 23:06:59 +03:00
|
|
|
|
|
|
|
void visitModule(Module &M) {
|
|
|
|
for (GlobalVariable &GV : M.getGlobalList())
|
|
|
|
visitGlobalVariable(GV);
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitGlobalVariable(GlobalVariable &GV) {
|
|
|
|
// Global variables only have one attribute set.
|
|
|
|
const AttributeSet &AS = GV.getAttributes();
|
|
|
|
if (AS.hasAttributes())
|
|
|
|
visitAttributeSet(AS, GlobalVariablesToRefine[&GV]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitFunction(Function &F) {
|
|
|
|
if (F.getIntrinsicID() != Intrinsic::not_intrinsic)
|
|
|
|
return; // We can neither add nor remove attributes from intrinsics.
|
|
|
|
visitAttributeList(F.getAttributes(), FunctionsToRefine[&F]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitCallBase(CallBase &I) {
|
|
|
|
visitAttributeList(I.getAttributes(), CallsToRefine[&I]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void visitAttributeList(const AttributeList &AL,
|
|
|
|
AttrPtrVecVecTy &AttributeSetsToPreserve) {
|
|
|
|
assert(AttributeSetsToPreserve.empty() && "Should not be sharing vectors.");
|
|
|
|
AttributeSetsToPreserve.reserve(AL.getNumAttrSets());
|
2021-09-30 13:57:55 -07:00
|
|
|
for (unsigned SetIdx : AL.indexes()) {
|
[llvm-reduce] Reducing attributes
Summary:
This handles all three places where attributes could currently be - `GlobalVariable`, `Function` and `CallBase`.
For last two, it correctly handles all three possible attribute locations (return value, arguments and function itself)
There was a previous attempt at it D73853,
which was committed in rGfc62b36a000681c01e993242b583c5ec4ab48a3c,
but then reverted all the way back in rGb12176d2aafa0ccb2585aa218fc3b454ba84f2a9
due to some (osx?) test failures.
Reviewers: nickdesaulniers, dblaikie, diegotf, george.burgess.iv, jdoerfert, Tyker, arsenm
Reviewed By: nickdesaulniers
Subscribers: wdng, MaskRay, arsenm, llvm-commits, mgorny
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D83351
2020-07-09 23:06:59 +03:00
|
|
|
AttrPtrIdxVecVecTy AttributesToPreserve;
|
|
|
|
AttributesToPreserve.first = SetIdx;
|
|
|
|
visitAttributeSet(AL.getAttributes(AttributesToPreserve.first),
|
|
|
|
AttributesToPreserve.second);
|
|
|
|
if (!AttributesToPreserve.second.empty())
|
|
|
|
AttributeSetsToPreserve.emplace_back(std::move(AttributesToPreserve));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 10:55:59 -05:00
|
|
|
// FIXME: Should just directly use AttrBuilder instead of going through
|
|
|
|
// AttrPtrVecTy
|
[llvm-reduce] Reducing attributes
Summary:
This handles all three places where attributes could currently be - `GlobalVariable`, `Function` and `CallBase`.
For last two, it correctly handles all three possible attribute locations (return value, arguments and function itself)
There was a previous attempt at it D73853,
which was committed in rGfc62b36a000681c01e993242b583c5ec4ab48a3c,
but then reverted all the way back in rGb12176d2aafa0ccb2585aa218fc3b454ba84f2a9
due to some (osx?) test failures.
Reviewers: nickdesaulniers, dblaikie, diegotf, george.burgess.iv, jdoerfert, Tyker, arsenm
Reviewed By: nickdesaulniers
Subscribers: wdng, MaskRay, arsenm, llvm-commits, mgorny
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D83351
2020-07-09 23:06:59 +03:00
|
|
|
void visitAttributeSet(const AttributeSet &AS,
|
|
|
|
AttrPtrVecTy &AttrsToPreserve) {
|
|
|
|
assert(AttrsToPreserve.empty() && "Should not be sharing vectors.");
|
|
|
|
AttrsToPreserve.reserve(AS.getNumAttributes());
|
2023-01-02 10:55:59 -05:00
|
|
|
|
|
|
|
// 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.emplace_back(NoInline);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Attribute A : AS) {
|
|
|
|
if (A.isEnumAttribute()) {
|
|
|
|
Attribute::AttrKind Kind = A.getKindAsEnum();
|
|
|
|
if (Kind == Attribute::NoInline)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (RemoveNoInline && Kind == Attribute::OptimizeNone)
|
|
|
|
continue;
|
2023-01-03 08:00:03 -05:00
|
|
|
|
|
|
|
// TODO: Could only remove this if there are no constrained calls in the
|
|
|
|
// function.
|
|
|
|
if (Kind == Attribute::StrictFP) {
|
|
|
|
AttrsToPreserve.emplace_back(A);
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-02 10:55:59 -05:00
|
|
|
}
|
|
|
|
|
[llvm-reduce] Reducing attributes
Summary:
This handles all three places where attributes could currently be - `GlobalVariable`, `Function` and `CallBase`.
For last two, it correctly handles all three possible attribute locations (return value, arguments and function itself)
There was a previous attempt at it D73853,
which was committed in rGfc62b36a000681c01e993242b583c5ec4ab48a3c,
but then reverted all the way back in rGb12176d2aafa0ccb2585aa218fc3b454ba84f2a9
due to some (osx?) test failures.
Reviewers: nickdesaulniers, dblaikie, diegotf, george.burgess.iv, jdoerfert, Tyker, arsenm
Reviewed By: nickdesaulniers
Subscribers: wdng, MaskRay, arsenm, llvm-commits, mgorny
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D83351
2020-07-09 23:06:59 +03:00
|
|
|
if (O.shouldKeep())
|
2023-01-02 10:55:59 -05:00
|
|
|
AttrsToPreserve.emplace_back(A);
|
|
|
|
}
|
[llvm-reduce] Reducing attributes
Summary:
This handles all three places where attributes could currently be - `GlobalVariable`, `Function` and `CallBase`.
For last two, it correctly handles all three possible attribute locations (return value, arguments and function itself)
There was a previous attempt at it D73853,
which was committed in rGfc62b36a000681c01e993242b583c5ec4ab48a3c,
but then reverted all the way back in rGb12176d2aafa0ccb2585aa218fc3b454ba84f2a9
due to some (osx?) test failures.
Reviewers: nickdesaulniers, dblaikie, diegotf, george.burgess.iv, jdoerfert, Tyker, arsenm
Reviewed By: nickdesaulniers
Subscribers: wdng, MaskRay, arsenm, llvm-commits, mgorny
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D83351
2020-07-09 23:06:59 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-01-02 10:55:59 -05:00
|
|
|
AttributeSet convertAttributeRefToAttributeSet(LLVMContext &C,
|
|
|
|
ArrayRef<Attribute> Attributes) {
|
2022-01-03 13:32:19 -05:00
|
|
|
AttrBuilder B(C);
|
2023-01-02 10:55:59 -05:00
|
|
|
for (Attribute A : Attributes)
|
|
|
|
B.addAttribute(A);
|
[llvm-reduce] Reducing attributes
Summary:
This handles all three places where attributes could currently be - `GlobalVariable`, `Function` and `CallBase`.
For last two, it correctly handles all three possible attribute locations (return value, arguments and function itself)
There was a previous attempt at it D73853,
which was committed in rGfc62b36a000681c01e993242b583c5ec4ab48a3c,
but then reverted all the way back in rGb12176d2aafa0ccb2585aa218fc3b454ba84f2a9
due to some (osx?) test failures.
Reviewers: nickdesaulniers, dblaikie, diegotf, george.burgess.iv, jdoerfert, Tyker, arsenm
Reviewed By: nickdesaulniers
Subscribers: wdng, MaskRay, arsenm, llvm-commits, mgorny
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D83351
2020-07-09 23:06:59 +03:00
|
|
|
return AttributeSet::get(C, B);
|
|
|
|
}
|
|
|
|
|
|
|
|
AttributeList convertAttributeRefVecToAttributeList(
|
|
|
|
LLVMContext &C, ArrayRef<AttrPtrIdxVecVecTy> AttributeSets) {
|
|
|
|
std::vector<std::pair<unsigned, AttributeSet>> SetVec;
|
|
|
|
SetVec.reserve(AttributeSets.size());
|
|
|
|
|
|
|
|
transform(AttributeSets, std::back_inserter(SetVec),
|
|
|
|
[&C](const AttrPtrIdxVecVecTy &V) {
|
|
|
|
return std::make_pair(
|
|
|
|
V.first, convertAttributeRefToAttributeSet(C, V.second));
|
|
|
|
});
|
|
|
|
|
2022-06-04 21:23:18 -07:00
|
|
|
llvm::sort(SetVec, llvm::less_first()); // All values are unique.
|
[llvm-reduce] Reducing attributes
Summary:
This handles all three places where attributes could currently be - `GlobalVariable`, `Function` and `CallBase`.
For last two, it correctly handles all three possible attribute locations (return value, arguments and function itself)
There was a previous attempt at it D73853,
which was committed in rGfc62b36a000681c01e993242b583c5ec4ab48a3c,
but then reverted all the way back in rGb12176d2aafa0ccb2585aa218fc3b454ba84f2a9
due to some (osx?) test failures.
Reviewers: nickdesaulniers, dblaikie, diegotf, george.burgess.iv, jdoerfert, Tyker, arsenm
Reviewed By: nickdesaulniers
Subscribers: wdng, MaskRay, arsenm, llvm-commits, mgorny
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D83351
2020-07-09 23:06:59 +03:00
|
|
|
|
|
|
|
return AttributeList::get(C, SetVec);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes out-of-chunk attributes from module.
|
2021-10-05 00:05:37 -07:00
|
|
|
static void extractAttributesFromModule(Oracle &O, Module &Program) {
|
|
|
|
AttributeRemapper R(O);
|
[llvm-reduce] Reducing attributes
Summary:
This handles all three places where attributes could currently be - `GlobalVariable`, `Function` and `CallBase`.
For last two, it correctly handles all three possible attribute locations (return value, arguments and function itself)
There was a previous attempt at it D73853,
which was committed in rGfc62b36a000681c01e993242b583c5ec4ab48a3c,
but then reverted all the way back in rGb12176d2aafa0ccb2585aa218fc3b454ba84f2a9
due to some (osx?) test failures.
Reviewers: nickdesaulniers, dblaikie, diegotf, george.burgess.iv, jdoerfert, Tyker, arsenm
Reviewed By: nickdesaulniers
Subscribers: wdng, MaskRay, arsenm, llvm-commits, mgorny
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D83351
2020-07-09 23:06:59 +03:00
|
|
|
R.visit(Program);
|
|
|
|
|
2021-10-05 00:05:37 -07:00
|
|
|
LLVMContext &C = Program.getContext();
|
[llvm-reduce] Reducing attributes
Summary:
This handles all three places where attributes could currently be - `GlobalVariable`, `Function` and `CallBase`.
For last two, it correctly handles all three possible attribute locations (return value, arguments and function itself)
There was a previous attempt at it D73853,
which was committed in rGfc62b36a000681c01e993242b583c5ec4ab48a3c,
but then reverted all the way back in rGb12176d2aafa0ccb2585aa218fc3b454ba84f2a9
due to some (osx?) test failures.
Reviewers: nickdesaulniers, dblaikie, diegotf, george.burgess.iv, jdoerfert, Tyker, arsenm
Reviewed By: nickdesaulniers
Subscribers: wdng, MaskRay, arsenm, llvm-commits, mgorny
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D83351
2020-07-09 23:06:59 +03:00
|
|
|
for (const auto &I : R.GlobalVariablesToRefine)
|
|
|
|
I.first->setAttributes(convertAttributeRefToAttributeSet(C, I.second));
|
|
|
|
for (const auto &I : R.FunctionsToRefine)
|
|
|
|
I.first->setAttributes(convertAttributeRefVecToAttributeList(C, I.second));
|
|
|
|
for (const auto &I : R.CallsToRefine)
|
|
|
|
I.first->setAttributes(convertAttributeRefVecToAttributeList(C, I.second));
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::reduceAttributesDeltaPass(TestRunner &Test) {
|
2022-10-17 21:43:08 -07:00
|
|
|
runDeltaPass(Test, extractAttributesFromModule, "Reducing Attributes");
|
[llvm-reduce] Reducing attributes
Summary:
This handles all three places where attributes could currently be - `GlobalVariable`, `Function` and `CallBase`.
For last two, it correctly handles all three possible attribute locations (return value, arguments and function itself)
There was a previous attempt at it D73853,
which was committed in rGfc62b36a000681c01e993242b583c5ec4ab48a3c,
but then reverted all the way back in rGb12176d2aafa0ccb2585aa218fc3b454ba84f2a9
due to some (osx?) test failures.
Reviewers: nickdesaulniers, dblaikie, diegotf, george.burgess.iv, jdoerfert, Tyker, arsenm
Reviewed By: nickdesaulniers
Subscribers: wdng, MaskRay, arsenm, llvm-commits, mgorny
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D83351
2020-07-09 23:06:59 +03:00
|
|
|
}
|