File: Printf.h

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 (93 lines) | stat: -rw-r--r-- 3,745 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
/*========================== begin_copyright_notice ============================

Copyright (C) 2021-2024 Intel Corporation

SPDX-License-Identifier: MIT

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

#ifndef VC_UTILS_GENX_PRINTF_H
#define VC_UTILS_GENX_PRINTF_H

#include <llvm/ADT/Optional.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/Operator.h>
#include <llvm/IR/Value.h>

#include <vector>

namespace vc {

// A name for an attribute to mark global variables which are strings that
// are used in printf builtin. Format strings and strings passed through %s
// modifier should be marked.
inline constexpr const char PrintfStringVariable[] = "VCPrintfStringVariable";
// A message to display when compiler cannot reach printf string.
inline constexpr const char PrintfStringAccessError[] =
    "Too entangled string access in printf, the compiler cannot resolve it in "
    "compile time";

// Checks whether global variable is constant and has [N x i8] type.
bool isConstantString(const llvm::GlobalVariable &GV);
bool isConstantString(const llvm::Value &V);

// Checks whether GEP takes pointer to the first element of a constant string.
bool isConstantStringFirstElementGEP(const llvm::GEPOperator &GEP);
bool isConstantStringFirstElementGEP(const llvm::Value &V);

// Returnes global string variable that \p Op points to. \p Op must be a
// pointer to i8*. Optional variant can return nullptr when there's no such
// global variable. Standard variant requires from user to provide a correct
// pointer so it points to a string.
const llvm::GlobalVariable *
getConstStringGVFromOperandOptional(const llvm::Value &Op);
llvm::GlobalVariable *getConstStringGVFromOperandOptional(llvm::Value &Op);
const llvm::GlobalVariable &getConstStringGVFromOperand(const llvm::Value &Op);
llvm::GlobalVariable &getConstStringGVFromOperand(llvm::Value &Op);

llvm::Optional<llvm::StringRef>
getConstStringFromOperandOptional(const llvm::Value &Op);
llvm::StringRef getConstStringFromOperand(const llvm::Value &Op);

// Information about a single printf argument.
struct PrintfArgInfo {
  enum TypeKind { Char, Short, Int, Long, Double, Pointer, String };
  TypeKind Type;
  // For those types where signedness is not applicable false should be set.
  bool IsSigned = false;
};

using PrintfArgInfoSeq = std::vector<PrintfArgInfo>;

// Minimal parsing of printf format string. Only types of arguments are defined.
PrintfArgInfoSeq parseFormatString(llvm::StringRef FmtStr);

// Whether \p Usr is `@llvm.vc.internal.print.format.index` inrinsic call.
bool isPrintFormatIndex(const llvm::User &Usr);

// Creates @llvm.vc.internal.print.format.index inrinsic call with \p Pointer as
// an operand. The call is inserted before \p InsertionPt.
llvm::CallInst &createPrintFormatIndex(llvm::Value &Pointer,
                                       llvm::Instruction &InsertionPt);

// There's a special case of GEP when all its users are
// vc.internal.print.format.index intrinsics. Such GEPs live until CisaBuilder
// and then handled as part of the intrinsic. This function checks whether \p
// GEP is a such GEP.
bool isLegalPrintFormatIndexGEP(const llvm::GEPOperator &GEP);
bool isLegalPrintFormatIndexGEP(const llvm::Value &V);

// Checks whether GEP with some format index users is provided.
// Unlike isLegalPrintFormatIndexGEP this function doesn't require all users to
// be format indices.
bool isPrintFormatIndexGEP(const llvm::Value &V);
bool isPrintFormatIndexGEP(const llvm::GEPOperator &V);

static inline bool isPrintfName(llvm::StringRef Name) {
  return Name == "printf" || Name.contains("__spirv_ocl_printf");
}

} // namespace vc

#endif // VC_UTILS_GENX_PRINTF_H