File: ShaderDump.cpp

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (79 lines) | stat: -rw-r--r-- 2,561 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2020-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "vc/Support/ShaderDump.h"
#include "vc/Support/BackendConfig.h"

#include <llvm/ADT/ArrayRef.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/IR/Module.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/raw_ostream.h>

using namespace llvm;

namespace {
class FileShaderDumper : public vc::ShaderDumper {
public:
  void dumpBinary(ArrayRef<char> Binary, StringRef DumpName) override {
    writeToFile(DumpName, [Binary](llvm::raw_fd_ostream &OS) {
      OS.write(Binary.data(), Binary.size());
    });
  }
  void dumpText(StringRef Text, StringRef DumpName) override {
    writeToFile(DumpName, [Text](llvm::raw_fd_ostream &OS) { OS << Text; });
  }
  void dumpModule(const Module &M, StringRef DumpName) override {
    writeToFile(DumpName,
                [&M](llvm::raw_fd_ostream &OS) { M.print(OS, nullptr); });
  }

  void dumpCos(StringRef Contents, StringRef DumpName) override {
    dumpText(Contents, DumpName);
  }

private:
  template <typename F> void writeToFile(StringRef DumpName, F Writer) const {
    int FD;
    auto EC = llvm::sys::fs::openFileForWrite(DumpName, FD);
    // Silently return, nothing critical if debug dump fails.
    if (EC)
      return;
    llvm::raw_fd_ostream Stream{FD, /*shouldClose=*/true};
    Writer(Stream);
  }
};
} // namespace

std::unique_ptr<vc::ShaderDumper> vc::createDefaultShaderDumper() {
  return std::make_unique<FileShaderDumper>();
}

static void writeContentToFile(const Twine &Name, llvm::ArrayRef<char> Blob) {
  std::error_code EC;
  // no error handling since this is debug output
  raw_fd_ostream OS(Name.str(), EC);
  OS.write(Blob.data(), Blob.size());
}

std::string vc::legalizeShaderDumpName(const llvm::Twine &FileName) {
  std::string Result = FileName.str();
  std::replace_if(Result.begin(), Result.end(),
                  [](auto c) { return (!std::isalnum(c) && c != '.'); }, '_');
  return Result;
}

void vc::produceAuxiliaryShaderDumpFile(const llvm::GenXBackendConfig &BC,
                                        const llvm::Twine &OutputName,
                                        llvm::ArrayRef<char> Blob) {
  std::string LegalizedName = legalizeShaderDumpName(OutputName);
  if (BC.hasShaderDumper())
    BC.getShaderDumper().dumpBinary(Blob, LegalizedName);
  else
    writeContentToFile(LegalizedName, Blob);
}