llvm_util.h revision 1e67c90e2caceeff82d09793d1ef5fa0300d219b
1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations under the License. 14==============================================================================*/ 15 16#ifndef TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_LLVM_UTIL_H_ 17#define TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_LLVM_UTIL_H_ 18 19#include <stdint.h> 20#include <string> 21#include <vector> 22 23#include "external/llvm/include/llvm/ADT/StringRef.h" 24#include "external/llvm/include/llvm/IR/BasicBlock.h" 25#include "external/llvm/include/llvm/IR/IRBuilder.h" 26#include "external/llvm/include/llvm/IR/Instructions.h" 27#include "external/llvm/include/llvm/IR/Module.h" 28#include "external/llvm/include/llvm/IR/Value.h" 29#include "external/llvm/include/llvm/Support/raw_ostream.h" 30#include "tensorflow/compiler/xla/service/buffer_assignment.h" 31#include "tensorflow/compiler/xla/types.h" 32#include "tensorflow/compiler/xla/xla_data.pb.h" 33#include "tensorflow/core/lib/core/stringpiece.h" 34#include "tensorflow/core/lib/gtl/array_slice.h" 35#include "tensorflow/core/platform/types.h" 36 37namespace llvm { 38class FastMathFlags; 39class TargetOptions; 40}; 41 42namespace xla { 43namespace llvm_ir { 44 45// Convert a std::string (used by LLVM's interfaces) to string. 46string AsString(const std::string& str); 47 48// Convert a tensorflow::StringPiece to a llvm::StringRef. Note: both 49// tensorflow::StringPiece and llvm::StringRef are non-owning pointers into a 50// string in memory. This method is used to feed strings to LLVM 51// & Clang APIs that expect llvm::StringRef. 52llvm::StringRef AsStringRef(tensorflow::StringPiece str); 53 54template <typename T> 55llvm::ArrayRef<T> AsArrayRef(const std::vector<T>& vec) { 56 return llvm::ArrayRef<T>(vec.data(), vec.size()); 57} 58 59template <typename T> 60llvm::ArrayRef<T> AsArrayRef(const tensorflow::gtl::ArraySlice<T>& slice) { 61 return llvm::ArrayRef<T>(slice.data(), slice.size()); 62} 63 64// Dump the given LLVM entity to a string. This works for Types and Values. 65template <typename T> 66string DumpToString(const T& entity) { 67 std::string buffer_string; 68 llvm::raw_string_ostream ostream(buffer_string); 69 entity.print(ostream); 70 ostream.flush(); 71 return AsString(buffer_string); 72} 73 74// Dump the given LLVM module to a string. This requires a function distinct 75// from DumpToString because the signatures of the print() methods for Values 76// and Modules are slightly different. 77string DumpModuleToString(const llvm::Module& module); 78 79// Sanitizes the given name to be a valid LLVM IR value name. 80string SanitizeIrName(string name); 81 82// Emits a call to the specified intrinsic with the given operands. Overloaded 83// intrinsics (for example, "minnum") must include a type in overloaded_types 84// for each overloaded type. Typically, overloaded intrinsics have only a single 85// overloaded type. 86llvm::Value* EmitCallToIntrinsic( 87 llvm::Intrinsic::ID intrinsic_id, 88 tensorflow::gtl::ArraySlice<llvm::Value*> operands, 89 tensorflow::gtl::ArraySlice<llvm::Type*> overloaded_types, 90 llvm::IRBuilder<>* ir_builder); 91 92// Convenience methods for emitting a GEP instruction that indexes into a buffer 93// (1-dimensional array), equivalent to array[index]. The type is automatically 94// determined from the element type of the array. The int64 index overload 95// wraps the index in a i64 llvm::Value. 96llvm::Value* EmitBufferIndexingGEP(llvm::Value* array, llvm::Value* index, 97 llvm::IRBuilder<>* ir_builder); 98llvm::Value* EmitBufferIndexingGEP(llvm::Value* array, int64 index, 99 llvm::IRBuilder<>* ir_builder); 100 101// Returns the LLVM type which represents the given XLA primitive type. 102llvm::Type* PrimitiveTypeToIrType(PrimitiveType element_type, 103 llvm::IRBuilder<>* ir_builder); 104 105// Returns the LLVM type which represents the given XLA shape. For example, 106// if "shape" is [5 x [10 x f32]], the function returns [5 x [10 x float]]. 107llvm::Type* ShapeToIrType(const Shape& shape, llvm::IRBuilder<>* ir_builder); 108 109// Converts a given literal to an IR Constant. Literals have known constant 110// values at IR emission time. 111llvm::Constant* ConvertLiteralToIrConstant(const Literal& literal, 112 llvm::IRBuilder<>* ir_builder); 113 114// Inserts an allocate of the requested type at the entry point of the 115// function that the builder is currently building. The insert point 116// of the builder is set to the same place after calling this function 117// as before. 118// 119// This can be useful to avoid e.g. executing an alloca every time 120// through a loop. 121llvm::AllocaInst* EmitAllocaAtFunctionEntry(llvm::Type* type, 122 tensorflow::StringPiece name, 123 llvm::IRBuilder<>* ir_builder, 124 int alignment = 0); 125 126// As EmitAllocaAtFunctionEntry, but allocates element_count entries 127// intead of a single element. 128llvm::AllocaInst* EmitAllocaAtFunctionEntryWithCount( 129 llvm::Type* type, llvm::Value* element_count, tensorflow::StringPiece name, 130 llvm::IRBuilder<>* ir_builder, int alignment = 0); 131 132// Creates a basic block with the same context and funtion as for the 133// builder. Inserts at the end of the function if insert_before is 134// null. 135llvm::BasicBlock* CreateBasicBlock(llvm::BasicBlock* insert_before, 136 tensorflow::StringPiece name, 137 llvm::IRBuilder<>* ir_builder); 138 139// Struct with data on a conditional branch in a diamond shape created 140// via EmitIfThenElse. 141struct LlvmIfData { 142 // The block that has the conditional branch. 143 llvm::BasicBlock* if_block; 144 145 // The block that is executed if the condition is true. 146 llvm::BasicBlock* true_block; 147 148 // The block that is executed if the condition is false. 149 llvm::BasicBlock* false_block; 150 151 // The block that follows after both the true_block and the 152 // false_block. 153 llvm::BasicBlock* after_block; 154}; 155 156// Inserts a diamond-shaped if-then-else construct at the current 157// insertion point of the builder. This involves splitting the current 158// block into two blocks, at the insertion point, and introducing a 159// true-block and a false-block that connect the two split pieces. The 160// true-block is executed if the condition parameter evaluates to true 161// and otherwise the false-block is executed. If `emit_else` is false, 162// it jumps to the after-block rather than the false-block if the 163// condition is false, and the returned `false_block` is null. 164// 165// Currently the insertion point of the builder must be a well-formed 166// block with a terminator. If you need to use this for a 167// non-terminated block, just make the function able to do that too. 168LlvmIfData EmitIfThenElse(llvm::Value* condition, tensorflow::StringPiece name, 169 llvm::IRBuilder<>* ir_builder, bool emit_else = true); 170 171// Emits a compare operation between "lhs" and "rhs" with the given predicate, 172// and then converts the result to i8 so that it is addressable. 173llvm::Value* EmitComparison(llvm::CmpInst::Predicate predicate, 174 llvm::Value* lhs, llvm::Value* rhs, 175 llvm::IRBuilder<>* ir_builder); 176 177// Emits a call that logs the given value with the given tag as a prefix. 178// The provided tag and value are passed to a runtime logging call that is 179// embedded in this translation unit when the emitted code is executed. 180// 181// This can be very useful for debugging generated programs in short order when 182// developing new generated routines. 183// 184// Precondition: value must be an int64. 185// Precondition: tag must be a stable pointer for the lifetime of the generated 186// program (the constant pointer is burned in to the program). 187void EmitLogging(const char* tag, llvm::Value* value, 188 llvm::IRBuilder<>* ir_builder); 189 190// Adds TBAA metadata to a load or store instruction using the given shape as 191// it's type. The is_pointer_to parameter is used to indicate whether or not 192// this instruction loads or stores a pointer to an array. 193void SetTbaaForInstruction(llvm::Instruction* instruction, Shape shape, 194 bool is_pointer_to); 195 196// Adds alignment metadata to a load instruction using the given alignment. 197// The alignment refers to the result of the load, not the load itself. 198void SetAlignmentMetadataForLoad(llvm::LoadInst* load, uint64_t alignment); 199 200// Adds dereferenceable metadata to a load instruction using the given 201// the number of dereferenceable bytes. 202// Dereferenceable refers to the result of the load, not the load itself. 203void SetDereferenceableMetadataForLoad(llvm::LoadInst* load, 204 uint64_t dereferenceable_bytes); 205 206// Tells LLVM `inst >= lower && inst < upper`. Returns `inst` for convenience. 207llvm::Instruction* AddRangeMetadata(int64 lower, int64 upper, 208 llvm::Instruction* inst); 209 210void SetToFirstInsertPoint(llvm::BasicBlock* blk, llvm::IRBuilder<>* builder); 211 212// Create a bitwise rotation of `rotand` by `rotor`. 213llvm::Value* CreateRor(llvm::Value* rotand, llvm::Value* rotor, 214 llvm::IRBuilder<>* builder); 215 216// Returns the number of bytes within the shape. 217int64 ByteSizeOf(const Shape& shape, const llvm::DataLayout& data_layout); 218 219// Set values in the given FastMathFlags struct according to our XLA flags. 220void SetFastMathFlags(llvm::FastMathFlags* flags); 221 222// Set values in the given TargetOptions struct according to our XLA flags. 223void SetTargetOptions(llvm::TargetOptions* options); 224 225} // namespace llvm_ir 226} // namespace xla 227 228#endif // TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_LLVM_UTIL_H_ 229