File: Utils.hpp

package info (click to toggle)
intel-graphics-compiler2 2.28.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 792,744 kB
  • sloc: cpp: 5,761,745; ansic: 466,928; lisp: 312,143; python: 114,790; asm: 44,736; pascal: 10,930; sh: 8,033; perl: 7,914; ml: 3,625; awk: 3,523; yacc: 2,747; javascript: 2,667; lex: 1,898; f90: 1,028; cs: 573; xml: 474; makefile: 344; objc: 162
file content (102 lines) | stat: -rw-r--r-- 3,203 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
/*========================== 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