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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
// LLVM module pass. Sets AlwaysInline on functions that have arguments whose size
// exceeds the largest available register size of the Target
//
// Copyright (c) 2023 Michal Babej / Intel Finland Oy
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "CompilerWarnings.h"
IGNORE_COMPILER_WARNING("-Wmaybe-uninitialized")
#include <llvm/ADT/Twine.h>
POP_COMPILER_DIAGS
IGNORE_COMPILER_WARNING("-Wunused-parameter")
#include <llvm/ADT/SmallPtrSet.h>
#include <llvm/IR/DataLayout.h>
#include <llvm/IR/Module.h>
#include <llvm/Pass.h>
#include <llvm/Support/CommandLine.h>
#include "LLVMUtils.h"
#include "MinLegalVecSize.hh"
#include "pocl_llvm_api.h"
POP_COMPILER_DIAGS
#include <iostream>
#include <set>
#include <string>
//#define DEBUG_VEC_SIZE
#define PASS_NAME "fix-min-legal-vec-size"
#define PASS_CLASS pocl::FixMinVecSize
#define PASS_DESC "Module pass stuff"
namespace pocl {
using namespace llvm;
static void setFunctionMinLegalVecAttr(llvm::Function *F, uint64_t Value) {
Attribute replacementAttr = Attribute::get(
F->getContext(), "min-legal-vector-width", std::to_string(Value));
F->setAttributes(
F->getAttributes()
.removeFnAttribute(F->getContext(), "min-legal-vector-width")
.addFnAttribute(F->getContext(), replacementAttr));
}
/* some functions have an incorrect value in attribute min-legal-vec-size,
* find out the true value here, fix & return it */
static uint64_t getMinVecSizeFromPrototype(llvm::Function *F,
const std::string &Spaces) {
uint64_t ReportedSize = 0;
// get the value from F Attribute
llvm::Attribute Attr = F->getFnAttribute("min-legal-vector-width");
if (Attr.isValid()) {
Attr.getValueAsString().getAsInteger(0, ReportedSize);
}
uint64_t Max = ReportedSize;
const DataLayout &DL = F->getParent()->getDataLayout();
if (F->getReturnType()->isSized()) {
TypeSize RetTySize = DL.getTypeAllocSizeInBits(F->getReturnType());
assert(RetTySize.isScalable() == false);
Max = std::max(Max, RetTySize.getFixedValue());
}
for (const llvm::Argument &A : F->args()) {
Type *T = nullptr;
if (A.hasByValAttr() && A.getType()->isPointerTy()) {
T = A.getParamByValType();
} else {
T = A.getType();
}
if (!T->isSized())
continue;
TypeSize ArgTySize = DL.getTypeAllocSizeInBits(T);
Max = std::max(Max, ArgTySize.getFixedValue());
}
if (Max > ReportedSize) {
#ifdef DEBUG_VEC_SIZE
std::cerr << Spaces << "Function " << F->getName().str()
<< " has wrong min-legal-vec-size, "
"reported: "
<< ReportedSize << ", actual: " << Max << "\n";
#endif
setFunctionMinLegalVecAttr(F, Max);
}
return Max;
}
/* recursively look & fix min-legal-vec-size */
static uint64_t getAndFixLargestVecSize(llvm::Function *F, unsigned Justify) {
SmallVector<llvm::Function *, 8> Calls;
std::string Spaces(Justify, ' ');
uint64_t ReportedSize = getMinVecSizeFromPrototype(F, Spaces);
uint64_t MinVecSize = ReportedSize;
// find called functions
for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE; ++BI) {
Instruction *Instr = dyn_cast<Instruction>(BI);
if (!llvm::isa<CallInst>(Instr))
continue;
CallInst *CallInstr = dyn_cast<CallInst>(Instr);
llvm::Function *Callee = CallInstr->getCalledFunction();
if (Callee == nullptr)
continue;
if (Callee->hasName() && Callee->getName().startswith("llvm."))
continue;
Calls.push_back(Callee);
}
}
#ifdef DEBUG_VEC_SIZE
std::cerr << Spaces << "Num Callees: " << Calls.size() << "\n";
#endif
for (llvm::Function *Callee : Calls) {
if (Callee->hasName()) {
std::string Name = Callee->getName().str();
#ifdef DEBUG_VEC_SIZE
std::cerr << Spaces << "Found callee: " << Name << "\n";
#endif
}
uint64_t Width = getAndFixLargestVecSize(Callee, Justify + 2);
if (Width > MinVecSize) {
MinVecSize = Width;
#ifdef DEBUG_VEC_SIZE
std::cerr << Spaces << "New min vec size: " << MinVecSize << "\n";
#endif
}
}
if (MinVecSize > ReportedSize) {
setFunctionMinLegalVecAttr(F, MinVecSize);
#ifdef DEBUG_VEC_SIZE
std::cerr << Spaces << "Fixed to: " << MinVecSize << "\n";
#endif
}
return MinVecSize;
}
// find all kernels with SPIR_KERNEL CC and recursively fix their
// "min-legal-vector-width" attributes to the correct value. If any
// called function has has "min-legal-vector-width" larger than
// the device's native vector width size, also set the AlwaysInline
// attribute on that function
// the kernels htemselves are
static bool fixMinVecSize(Module &M) {
unsigned long DeviceNativeVectorWidth = 0;
if (!getModuleIntMetadata(M, "device_native_vec_width",
DeviceNativeVectorWidth))
return false;
// fix the "min-legal-vector-width" attr recursively
for (llvm::Module::iterator i = M.begin(), e = M.end(); i != e; ++i) {
llvm::Function *F = &*i;
if (F->isDeclaration())
continue;
if (F->hasName() && F->getName().startswith("llvm."))
continue;
// AttributeSet Attrs;
if (pocl::isKernelToProcess(*F) &&
(F->getCallingConv() == llvm::CallingConv::SPIR_KERNEL)) {
#ifdef DEBUG_VEC_SIZE
std::cerr << "processing kernel: " << F->getName().str() << "\n";
#endif
llvm::Attribute Attr = F->getFnAttribute("min-legal-vector-width");
if (Attr.isValid()) {
continue;
}
getAndFixLargestVecSize(F, 0);
Attr = F->getFnAttribute("min-legal-vector-width");
uint64_t NewWidth = 0;
Attr.getValueAsString().getAsInteger(0, NewWidth);
#ifdef DEBUG_VEC_SIZE
std::cerr << "Set kernel attr min-legal-vector-width to : " << NewWidth
<< "\n";
#endif
}
}
// add the alwaysInline attribute where required
for (llvm::Module::iterator i = M.begin(), e = M.end(); i != e; ++i) {
llvm::Function *F = &*i;
if (F->isDeclaration())
continue;
if (F->hasName() && F->getName().startswith("llvm."))
continue;
if (pocl::isKernelToProcess(*F))
continue;
llvm::Attribute Attr = F->getFnAttribute("min-legal-vector-width");
if (!Attr.isValid())
continue;
uint64_t FuncMinVecWidth = 0;
Attr.getValueAsString().getAsInteger(0, FuncMinVecWidth);
#ifdef DEBUG_VEC_SIZE
std::cerr << "Min Vec Width: " << FuncMinVecWidth << "\n";
#endif
// force-inline functions larger than max vector size
if (FuncMinVecWidth <= DeviceNativeVectorWidth)
continue;
#ifdef DEBUG_VEC_SIZE
std::cerr << "processing function: " << F->getName().str() << "\n";
std::cerr << "MinVecSize " << FuncMinVecWidth
<< " larger than NativeVec: " << DeviceNativeVectorWidth
<< ", force-inlining\n";
#endif
decltype(Attribute::AlwaysInline) replaceThisAttr, replacementAttr;
replaceThisAttr = Attribute::NoInline;
replacementAttr = Attribute::AlwaysInline;
F->setAttributes(F->getAttributes()
.removeFnAttribute(F->getContext(), replaceThisAttr)
.addFnAttribute(F->getContext(), replacementAttr));
}
return false;
}
#if LLVM_MAJOR < MIN_LLVM_NEW_PASSMANAGER
char FixMinVecSize::ID = 0;
bool FixMinVecSize::runOnModule(Module &M) { return fixMinVecSize(M); }
void FixMinVecSize::getAnalysisUsage(llvm::AnalysisUsage &AU) const {
AU.setPreservesAll();
}
REGISTER_OLD_MPASS(PASS_NAME, PASS_CLASS, PASS_DESC);
#else
llvm::PreservedAnalyses FixMinVecSize::run(llvm::Module &M,
llvm::ModuleAnalysisManager &AM) {
fixMinVecSize(M);
return PreservedAnalyses::all();
}
REGISTER_NEW_MPASS(PASS_NAME, PASS_CLASS, PASS_DESC);
#endif
} // namespace pocl
|