MLIR  22.0.0git
BasicPtxBuilderInterface.h
Go to the documentation of this file.
1 //===- BasicPtxBuilderInterface.td - PTX builder interface -*- tablegen -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Defines the interface to build PTX (Parallel Thread Execution) from NVVM Ops
10 // automatically. It is used by NVVM to LLVM pass.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef NVVM_DIALECT_NVVM_IR_BASICPTXBUILDERINTERFACE_H_
15 #define NVVM_DIALECT_NVVM_IR_BASICPTXBUILDERINTERFACE_H_
16 
19 #include "mlir/IR/PatternMatch.h"
20 #include "mlir/IR/Value.h"
21 #include "llvm/Support/LogicalResult.h"
22 
23 namespace mlir {
24 namespace NVVM {
25 /// Register read/write modifier to build constraint string for PTX inline
26 /// https://docs.nvidia.com/cuda/inline-ptx-assembly/index.html#parameters
27 enum class PTXRegisterMod {
28  /// Read register with no modifier
29  Read = 0,
30  /// Write register with '=' modifier
31  Write = 2,
32  /// ReadWrite register with '+' modifier.
33  /// Note that, this is not natively supported by LLVM, the Interface does
34  /// mapping
35  ReadWrite = 1,
36 };
37 
38 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
39  PTXRegisterMod mod) {
40  switch (mod) {
42  return os << "Read";
44  return os << "Write";
46  return os << "ReadWrite";
47  }
48  llvm_unreachable("Unknown PTXRegisterMod value");
49 }
50 } // namespace NVVM
51 } // namespace mlir
52 
53 /// Include the generated interface declarations.
54 #include "mlir/Dialect/LLVMIR/BasicPtxBuilderInterface.h.inc"
55 
56 namespace mlir {
57 
58 namespace NVVM {
59 
60 /// A class to build PTX assembly automatically. It is used by
61 /// BasicPtxBuilderInterface.
62 class PtxBuilder {
63  // The interface op that is used to build the PTX.
64  BasicPtxBuilderInterface interfaceOp;
65  // Rewriter to create new operations.
66  PatternRewriter &rewriter;
67  // The operands for the PTX instruction
68  SmallVector<Value> ptxOperands;
69  // Register constraints (read, write, readwrite) and register data types
70  std::string registerConstraints;
71  // Modifiers
72  SmallVector<PTXRegisterMod> registerModifiers;
73  // Has return value as write-only or read-write
74  bool hasResult = false;
75  // Indicates if the Op will handle the register mapping manually.
76  bool needsManualRegisterMapping = false;
77 
78 public:
79  /// Single constructor that only initializes members.
81  bool needsManualRegisterMapping = false)
82  : interfaceOp(op), rewriter(rewriter),
83  needsManualRegisterMapping(needsManualRegisterMapping) {}
84 
85  /// Add an operand with the read/write input type.
86  LogicalResult insertValue(Value v,
88 
89  /// Builds the inline assembly Op and returns it. The `insertValue` needs to
90  /// be called to pass operands before building the PTX.
91  LLVM::InlineAsmOp build();
92 
93  /// Shortcut to build the inline assembly Op and replace or erase the original
94  /// op with
95  void buildAndReplaceOp();
96 };
97 
98 /// Count the number of placeholder variables such as {$r}, {$w}, {$rw} in the
99 /// PTX code.
100 void countPlaceholderNumbers(StringRef ptxCode,
101  llvm::SmallDenseSet<unsigned> &seenRW,
102  llvm::SmallDenseSet<unsigned> &seenW,
103  llvm::SmallDenseSet<unsigned> &seenR,
107 
108 } // namespace NVVM
109 } // namespace mlir
110 
111 #endif // NVVM_DIALECT_NVVM_IR_BASICPTXBUILDERINTERFACE_H_
A class to build PTX assembly automatically.
PtxBuilder(Operation *op, PatternRewriter &rewriter, bool needsManualRegisterMapping=false)
Single constructor that only initializes members.
LogicalResult insertValue(Value v, PTXRegisterMod itype=PTXRegisterMod::Read)
Add an operand with the read/write input type.
LLVM::InlineAsmOp build()
Builds the inline assembly Op and returns it.
void buildAndReplaceOp()
Shortcut to build the inline assembly Op and replace or erase the original op with.
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:793
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
PTXRegisterMod
Register read/write modifier to build constraint string for PTX inline https://docs....
@ Write
Write register with '=' modifier.
@ ReadWrite
ReadWrite register with '+' modifier.
@ Read
Read register with no modifier.
llvm::raw_ostream & operator<<(llvm::raw_ostream &os, PTXRegisterMod mod)
void countPlaceholderNumbers(StringRef ptxCode, llvm::SmallDenseSet< unsigned > &seenRW, llvm::SmallDenseSet< unsigned > &seenW, llvm::SmallDenseSet< unsigned > &seenR, llvm::SmallVectorImpl< unsigned > &rwNums, llvm::SmallVectorImpl< unsigned > &wNums, llvm::SmallVectorImpl< unsigned > &rNums)
Count the number of placeholder variables such as {$r}, {$w}, {$rw} in the PTX code.
Include the generated interface declarations.