File: Utils.hpp

package info (click to toggle)
intel-graphics-compiler 1.0.17791.18-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 102,312 kB
  • sloc: cpp: 935,343; lisp: 286,143; ansic: 16,196; python: 3,279; yacc: 2,487; lex: 1,642; pascal: 300; sh: 174; makefile: 27
file content (111 lines) | stat: -rw-r--r-- 3,349 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2022 Intel Corporation

SPDX-License-Identifier: MIT

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

#pragma once

#include <algorithm>
#include <string>
#include <vector>

#include "Probe/Assertion.h"

#include "llvm/IR/Module.h"

namespace IGC {

template <class ContainerType, class BinaryFunction,
          class Sorter = std::less<typename ContainerType::key_type>>
static void OrderedTraversal(const ContainerType &Data, BinaryFunction Visit,
                             Sorter SortProcedure = {}) {
  std::vector<typename ContainerType::key_type> Keys;
  std::transform(Data.begin(), Data.end(), std::back_inserter(Keys),
                 [](const auto &KV) { return KV.first; });
  std::sort(Keys.begin(), Keys.end(), SortProcedure);
  for (const auto &Key : Keys) {
    auto FoundIt = Data.find(Key);
    IGC_ASSERT(FoundIt != Data.end());
    const auto &Val = FoundIt->second;
    Visit(Key, Val);
  }
}

constexpr int32_t SOURCE_LANG_LITERAL_MD_IS_NOT_PRESENT = -1;

//
// Flag "Source Lang Literal" contains sourceLanguage.
//
// Example:
//   !llvm.module.flags = !{..., !3, ...}
//   ...
//   !3 = !{i32 2, !"Source Lang Literal", 33}
//   ...
//
inline int32_t getSourceLangLiteralMDValueLegacy(const llvm::Module *module) {
  auto sourceLangLiteral = module->getModuleFlag("Source Lang Literal");
  if (!sourceLangLiteral) {
    return SOURCE_LANG_LITERAL_MD_IS_NOT_PRESENT;
  }

  auto constant = llvm::cast<llvm::ConstantAsMetadata>(sourceLangLiteral);
  return int32_t(
      llvm::cast<llvm::ConstantInt>(constant->getValue())->getZExtValue());
}

//
// Flag "Source Lang Literal" contains a list (MDTuple) of pairs (MDTuple):
//   (compileUnit, sourceLanguage)
//
// Example:
//   !llvm.module.flags = !{..., !3, ...}
//   ...
//   !3 = !{i32 2, !"Source Lang Literal", !4}
//   !4 = !{!5, !1834, ...}
//   !5 = !{!6, i32 33}
//   !6 = !DICompileUnit(...
//   ...
//   !1834 = !{!1835, i32 33}
//   !1835 = !DICompileUnit(...
//   ...
//
inline int32_t
getSourceLangLiteralMDValue(const llvm::DICompileUnit *compileUnit,
                            const llvm::Module *module) {
  const auto flagName = "Source Lang Literal";

  if (!module->getModuleFlag(flagName)) {
    return SOURCE_LANG_LITERAL_MD_IS_NOT_PRESENT;
  }

  auto node = llvm::dyn_cast<llvm::MDTuple>(module->getModuleFlag(flagName));
  if (!node) {
    return getSourceLangLiteralMDValueLegacy(module);
  }

  for (auto &op : node->operands()) {
    auto entry = llvm::cast<llvm::MDTuple>(op);
    if (llvm::cast<llvm::DICompileUnit>(entry->getOperand(0)) == compileUnit) {
      auto constant =
          llvm::cast<llvm::ConstantAsMetadata>(entry->getOperand(1));
      return int32_t(
          llvm::cast<llvm::ConstantInt>(constant->getValue())->getZExtValue());
    }
  }

  return SOURCE_LANG_LITERAL_MD_IS_NOT_PRESENT;
}

inline uint16_t getSourceLanguage(const llvm::DICompileUnit *compileUnit,
                                  const llvm::Module *module) {
  int32_t sourceLanguage = getSourceLangLiteralMDValue(compileUnit, module);
  if (sourceLanguage == SOURCE_LANG_LITERAL_MD_IS_NOT_PRESENT) {
    sourceLanguage = compileUnit->getSourceLanguage();
  }
  return uint16_t(sourceLanguage);
}

} // namespace IGC