File: AccuracyDecoratedCallsBiFResolution.cpp

package info (click to toggle)
intel-graphics-compiler2 2.16.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 106,644 kB
  • sloc: cpp: 805,640; lisp: 287,672; ansic: 16,414; python: 3,952; yacc: 2,588; lex: 1,666; pascal: 313; sh: 186; makefile: 35
file content (205 lines) | stat: -rw-r--r-- 7,502 bytes parent folder | download
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2024 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#include "AccuracyDecoratedCallsBiFResolution.hpp"

#include "llvmWrapper/IR/Type.h"
#include "llvm/IR/Attributes.h"
#include "Compiler/IGCPassSupport.h"
#include "Compiler/CodeGenPublic.h"
#include "Probe/Assertion.h"

using namespace IGC;
using namespace llvm;

// Register pass to igc-opt
#define PASS_FLAG "igc-accuracy-decorated-calls-bif-resolution"
#define PASS_DESCRIPTION "Accuracy decorated calls BiF resolution"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(AccuracyDecoratedCallsBiFResolution, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY,
                          PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
IGC_INITIALIZE_PASS_END(AccuracyDecoratedCallsBiFResolution, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

char AccuracyDecoratedCallsBiFResolution::ID = 0;

static inline const char *toString(Accuracy a) {
  switch (a) {
  default:
    return "";
  case HIGH_ACCURACY:
    return "high accuracy";
  case LOW_ACCURACY:
    return "low accuracy";
  case ENHANCED_PRECISION:
    return "enhanced precision";
  }
}

AccuracyDecoratedCallsBiFResolution::AccuracyDecoratedCallsBiFResolution() : ModulePass(ID) {
  initializeAccuracyDecoratedCallsBiFResolutionPass(*PassRegistry::getPassRegistry());
}

void AccuracyDecoratedCallsBiFResolution::initNameToBuiltinMap() {
#define DEF_NAME_TO_BUILTIN(name, accuracy, builtin) m_nameToBuiltin[name][accuracy] = builtin;
#include "NameToBuiltinDef.hpp"
#undef DEF_NAME_TO_BUILTIN
}

bool AccuracyDecoratedCallsBiFResolution::runOnModule(Module &M) {
  m_Module = static_cast<Module *>(&M);

  initNameToBuiltinMap();

  visit(M);

  return m_changed;
}

void AccuracyDecoratedCallsBiFResolution::visitBinaryOperator(BinaryOperator &inst) {
  // not supported by BiFModule yet;
  return;

  if (!inst.getType()->isFloatingPointTy())
    return;

  MDNode *MD = inst.getMetadata("fpbuiltin-max-error");
  if (!MD)
    return;

  IGC_ASSERT_MESSAGE(!IGCLLVM::isBFloatTy(inst.getType()),
                     "bfloat type is not supported with fpbuiltin-max-error decoration");
  if (IGCLLVM::isBFloatTy(inst.getType()))
    return;

  std::vector<Value *> args{};
  args.push_back(inst.getOperand(0));
  args.push_back(inst.getOperand(1));

  StringRef maxErrorStr = cast<MDString>(MD->getOperand(0))->getString();
  const std::string oldFuncName = inst.getOpcodeName();

  Instruction *currInst = cast<Instruction>(&inst);
  Function *newFunc =
      getOrInsertNewFunc(oldFuncName, inst.getType(), args, {}, CallingConv::SPIR_FUNC, maxErrorStr, currInst);

  CallInst *newCall = CallInst::Create(newFunc, args, inst.getName(), &inst);
  llvm::Attribute attr = llvm::Attribute::get(inst.getContext(), "fpbuiltin-max-error", maxErrorStr);
  newCall->addFnAttr(attr);

  inst.replaceAllUsesWith(newCall);
  inst.eraseFromParent();
}

void AccuracyDecoratedCallsBiFResolution::visitCallInst(CallInst &callInst) {
  AttributeList attributeList = callInst.getAttributes();
  AttributeSet callAttributes = attributeList.getFnAttrs();

  if (!callAttributes.hasAttribute("fpbuiltin-max-error"))
    return;

  Function *F = callInst.getCalledFunction();
  IGC_ASSERT_MESSAGE(nullptr != F, "Called function is null");
  if (!F)
    return;

  IGC_ASSERT_MESSAGE(!IGCLLVM::isBFloatTy(F->getType()),
                     "bfloat type is not supported with fpbuiltin-max-error decoration");
  if (IGCLLVM::isBFloatTy(F->getType()))
    return;

  std::vector<Value *> args{};
  for (const auto &arg : callInst.args())
    args.push_back(arg);

  llvm::Attribute maxErrAttr = callAttributes.getAttribute("fpbuiltin-max-error");
  StringRef maxErrorStr = maxErrAttr.getValueAsString();

  Instruction *currInst = cast<Instruction>(&callInst);
  Function *newFunc = getOrInsertNewFunc(F->getName(), F->getReturnType(), args, F->getAttributes(),
                                         F->getCallingConv(), maxErrorStr, currInst);

  CallInst *newCall = CallInst::Create(newFunc, args, callInst.getName(), &callInst);
  newCall->setCallingConv(callInst.getCallingConv());
  newCall->setAttributes(callInst.getAttributes());

  callInst.replaceAllUsesWith(newCall);
  callInst.eraseFromParent();
}

Function *AccuracyDecoratedCallsBiFResolution::getOrInsertNewFunc(
    const StringRef oldFuncName, Type *funcType, const ArrayRef<Value *> args, const AttributeList attributes,
    CallingConv::ID callingConv, const StringRef maxErrorStr, const Instruction *currInst) {
  double maxError = 0;
  maxErrorStr.getAsDouble(maxError);
  double ULPCutOff = funcType->isFloatTy() ? 0x1 << 12 : 0x1 << 26; // 2^12 : 2^26
  const Accuracy accuracy = getAccuracy(maxError, ULPCutOff, currInst);

  std::vector<Type *> argTypes{};
  for (const auto &arg : args)
    argTypes.push_back(arg->getType());

  FunctionType *FT = FunctionType::get(funcType, argTypes, false);
  std::string newFuncName = getFunctionName(oldFuncName, accuracy, currInst);
  Function *newFunc = cast<Function>(m_Module->getOrInsertFunction(newFuncName, FT, attributes).getCallee());
  IGC_ASSERT(nullptr != newFunc);
  newFunc->setCallingConv(callingConv);

  m_changed = true;
  return newFunc;
}

std::string AccuracyDecoratedCallsBiFResolution::getFunctionName(const StringRef oldFuncName, Accuracy accuracy,
                                                                 const Instruction *currInst) const {
  if (!m_nameToBuiltin.count(oldFuncName.str())) {
    // Temporary until we're certain of the mangling used for
    // sinpi cospi tanpi asinpi acospi atanpi sincos sincospi
    size_t idx = oldFuncName.find("_spirv_ocl_");
    IGC_ASSERT(StringRef::npos != idx);
    StringRef croppedName = oldFuncName.substr(idx);

    if (!m_nameToBuiltin.count(croppedName.str()))
      return oldFuncName.str(); // Function not found - don't change the name.

    // Function found in map with cropped name. Get the mapping.
    return getFunctionName(croppedName, accuracy, currInst);
  }

  if (!m_nameToBuiltin.at(oldFuncName.str()).count(accuracy)) {
    std::string warningMessage = "Built-in function not found for " + oldFuncName.str() + " at " + toString(accuracy) +
                                 ". Choosing higher precision.";
    getAnalysis<CodeGenContextWrapper>().getCodeGenContext()->EmitWarning(warningMessage.c_str(), currInst);

    switch (accuracy) {
    default:
      IGC_ASSERT_MESSAGE(false, "Unreachable, NameToBuiltinDef.hpp is likely broken");
      return oldFuncName.str();
    case ENHANCED_PRECISION:
      return getFunctionName(oldFuncName, LOW_ACCURACY, currInst);
    case LOW_ACCURACY:
      return getFunctionName(oldFuncName, HIGH_ACCURACY, currInst);
    }
  }
  return m_nameToBuiltin.at(oldFuncName.str()).at(accuracy);
}

Accuracy AccuracyDecoratedCallsBiFResolution::getAccuracy(double maxError, double cutOff,
                                                          const Instruction *currInst) const {
  if (maxError < 1.0)
    getAnalysis<CodeGenContextWrapper>().getCodeGenContext()->EmitError(
        "fpbuiltin-max-error can't have values below 1.0", currInst);

  if (maxError < 4.0)
    return HIGH_ACCURACY;

  if (maxError < cutOff) // 2^12 (for f32) or 2^26 (for f64)
    return LOW_ACCURACY;

  return ENHANCED_PRECISION;
}