Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:48:11

0001 //===--- RuntimeDebugBuilder.h --- Helper to insert prints into LLVM-IR ---===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 //===----------------------------------------------------------------------===//
0010 
0011 #ifndef RUNTIME_DEBUG_BUILDER_H
0012 #define RUNTIME_DEBUG_BUILDER_H
0013 
0014 #include "polly/CodeGen/IRBuilder.h"
0015 #include "llvm/ADT/ArrayRef.h"
0016 #include "llvm/ADT/StringRef.h"
0017 #include <vector>
0018 
0019 namespace llvm {
0020 class Value;
0021 class Function;
0022 } // namespace llvm
0023 
0024 namespace polly {
0025 
0026 /// Insert function calls that print certain LLVM values at run time.
0027 ///
0028 /// This class inserts libc function calls to print certain LLVM values at
0029 /// run time.
0030 struct RuntimeDebugBuilder {
0031 
0032   /// Generate a constant string into the builder's llvm::Module which can be
0033   /// passed to createCPUPrinter().
0034   ///
0035   /// @param Builder The builder used to emit the printer calls.
0036   /// @param Str     The string to be printed.
0037 
0038   /// @return        A global containing @p Str.
0039   static llvm::Value *getPrintableString(PollyIRBuilder &Builder,
0040                                          llvm::StringRef Str);
0041 
0042   /// Return whether an llvm::Value of the type @p Ty is printable for
0043   /// debugging.
0044   ///
0045   /// That is, whether such a value can be passed to createGPUPrinter()
0046   /// to be dumped as runtime.  If false is returned, those
0047   /// functions will fail.
0048   static bool isPrintable(llvm::Type *Ty);
0049 
0050   /// Print a set of LLVM-IR Values or StringRefs via printf
0051   ///
0052   ///  This function emits a call to printf that will print the given arguments.
0053   ///  It is useful for debugging CPU programs. All arguments given in this list
0054   ///  will be automatically concatenated and the resulting string will be
0055   ///  printed atomically. We also support ArrayRef arguments, which can be used
0056   ///  to provide of id values.
0057   ///
0058   ///  @param Builder The builder used to emit the printer calls.
0059   ///  @param Args    The list of values to print.
0060   template <typename... Args>
0061   static void createCPUPrinter(PollyIRBuilder &Builder, Args... args) {
0062     std::vector<llvm::Value *> Vector;
0063     createPrinter(Builder, Vector, args...);
0064   }
0065 
0066 private:
0067   /// Handle Values.
0068   template <typename... Args>
0069   static void createPrinter(PollyIRBuilder &Builder,
0070                             std::vector<llvm::Value *> &Values,
0071                             llvm::Value *Value, Args... args) {
0072     Values.push_back(Value);
0073     createPrinter(Builder, Values, args...);
0074   }
0075 
0076   /// Handle StringRefs.
0077   template <typename... Args>
0078   static void createPrinter(PollyIRBuilder &Builder,
0079                             std::vector<llvm::Value *> &Values,
0080                             llvm::StringRef String, Args... args) {
0081     Values.push_back(getPrintableString(Builder, String));
0082     createPrinter(Builder, Values, args...);
0083   }
0084 
0085   /// Handle ArrayRefs.
0086   template <typename... Args>
0087   static void createPrinter(PollyIRBuilder &Builder,
0088                             std::vector<llvm::Value *> &Values,
0089                             llvm::ArrayRef<llvm::Value *> Array, Args... args) {
0090     Values.insert(Values.end(), Array.begin(), Array.end());
0091     createPrinter(Builder, Values, args...);
0092   }
0093 
0094   /// Print a list of Values.
0095   static void createPrinter(PollyIRBuilder &Builder,
0096                             llvm::ArrayRef<llvm::Value *> Values);
0097 
0098   /// Print a list of Values on a CPU.
0099   static void createCPUPrinterT(PollyIRBuilder &Builder,
0100                                 llvm::ArrayRef<llvm::Value *> Values);
0101 
0102   /// Get a reference to the 'printf' function.
0103   ///
0104   /// If the current module does not yet contain a reference to printf, we
0105   /// insert a reference to it. Otherwise the existing reference is returned.
0106   static llvm::Function *getPrintF(PollyIRBuilder &Builder);
0107 
0108   /// Call printf
0109   ///
0110   /// @param Builder The builder used to insert the code.
0111   /// @param Format  The format string.
0112   /// @param Values  The set of values to print.
0113   static void createPrintF(PollyIRBuilder &Builder, std::string Format,
0114                            llvm::ArrayRef<llvm::Value *> Values);
0115 
0116   /// Get (and possibly insert) a vprintf declaration into the module.
0117   static llvm::Function *getVPrintF(PollyIRBuilder &Builder);
0118 
0119   /// Call fflush
0120   ///
0121   /// @param Builder The builder used to insert the code.
0122   static void createFlush(PollyIRBuilder &Builder);
0123 };
0124 } // namespace polly
0125 
0126 extern bool PollyDebugPrinting;
0127 
0128 #endif