summaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm18/include/llvm/IR/GenericConvergenceVerifierImpl.h
blob: 68f3f3565b6b50a5bb6ec62a6420913241e18d10 (plain) (blame)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#pragma once

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

//===- GenericConvergenceVerifierImpl.h -----------------------*- C++ -*---===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
///
/// A verifier for the static rules of convergence control tokens that works
/// with both LLVM IR and MIR.
///
/// This template implementation resides in a separate file so that it does not
/// get injected into every .cpp file that includes the generic header.
///
/// DO NOT INCLUDE THIS FILE WHEN MERELY USING CYCLEINFO.
///
/// This file should only be included by files that implement a
/// specialization of the relevant templates. Currently these are:
/// - llvm/lib/IR/Verifier.cpp
/// - llvm/lib/CodeGen/MachineVerifier.cpp
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_IR_GENERICCONVERGENCEVERIFIERIMPL_H
#define LLVM_IR_GENERICCONVERGENCEVERIFIERIMPL_H

#include "llvm/ADT/GenericConvergenceVerifier.h"
#include "llvm/ADT/PostOrderIterator.h"
#include "llvm/ADT/Twine.h"
#include "llvm/IR/IntrinsicInst.h"

#define Check(C, ...)                                                          \
  do {                                                                         \
    if (!(C)) {                                                                \
      reportFailure(__VA_ARGS__);                                              \
      return;                                                                  \
    }                                                                          \
  } while (false)

#define CheckOrNull(C, ...)                                                    \
  do {                                                                         \
    if (!(C)) {                                                                \
      reportFailure(__VA_ARGS__);                                              \
      return {};                                                               \
    }                                                                          \
  } while (false)

namespace llvm {
template <class ContextT> void GenericConvergenceVerifier<ContextT>::clear() {
  Tokens.clear();
  CI.clear();
  ConvergenceKind = NoConvergence;
}

template <class ContextT>
void GenericConvergenceVerifier<ContextT>::visit(const BlockT &BB) {
  SeenFirstConvOp = false;
}

template <class ContextT>
void GenericConvergenceVerifier<ContextT>::visit(const InstructionT &I) {
  auto ID = ContextT::getIntrinsicID(I);
  auto *TokenDef = findAndCheckConvergenceTokenUsed(I);
  bool IsCtrlIntrinsic = true;

  switch (ID) {
  case Intrinsic::experimental_convergence_entry:
    Check(isInsideConvergentFunction(I),
          "Entry intrinsic can occur only in a convergent function.",
          {Context.print(&I)});
    Check(I.getParent()->isEntryBlock(),
          "Entry intrinsic can occur only in the entry block.",
          {Context.print(&I)});
    Check(!SeenFirstConvOp,
          "Entry intrinsic cannot be preceded by a convergent operation in the "
          "same basic block.",
          {Context.print(&I)});
    LLVM_FALLTHROUGH;
  case Intrinsic::experimental_convergence_anchor:
    Check(!TokenDef,
          "Entry or anchor intrinsic cannot have a convergencectrl token "
          "operand.",
          {Context.print(&I)});
    break;
  case Intrinsic::experimental_convergence_loop:
    Check(TokenDef, "Loop intrinsic must have a convergencectrl token operand.",
          {Context.print(&I)});
    Check(!SeenFirstConvOp,
          "Loop intrinsic cannot be preceded by a convergent operation in the "
          "same basic block.",
          {Context.print(&I)});
    break;
  default:
    IsCtrlIntrinsic = false;
    break;
  }

  if (isConvergent(I))
    SeenFirstConvOp = true;

  if (TokenDef || IsCtrlIntrinsic) {
    Check(isConvergent(I),
          "Convergence control token can only be used in a convergent call.",
          {Context.print(&I)});
    Check(ConvergenceKind != UncontrolledConvergence,
          "Cannot mix controlled and uncontrolled convergence in the same "
          "function.",
          {Context.print(&I)});
    ConvergenceKind = ControlledConvergence;
  } else if (isConvergent(I)) {
    Check(ConvergenceKind != ControlledConvergence,
          "Cannot mix controlled and uncontrolled convergence in the same "
          "function.",
          {Context.print(&I)});
    ConvergenceKind = UncontrolledConvergence;
  }
}

template <class ContextT>
void GenericConvergenceVerifier<ContextT>::reportFailure(
    const Twine &Message, ArrayRef<Printable> DumpedValues) {
  FailureCB(Message);
  if (OS) {
    for (auto V : DumpedValues)
      *OS << V << '\n';
  }
}

template <class ContextT>
void GenericConvergenceVerifier<ContextT>::verify(const DominatorTreeT &DT) {
  assert(Context.getFunction());
  const auto &F = *Context.getFunction();

  DenseMap<const BlockT *, SmallVector<const InstructionT *, 8>> LiveTokenMap;
  DenseMap<const CycleT *, const InstructionT *> CycleHearts;

  // Just like the DominatorTree, compute the CycleInfo locally so that we
  // can run the verifier outside of a pass manager and we don't rely on
  // potentially out-dated analysis results.
  CI.compute(const_cast<FunctionT &>(F));

  auto checkToken = [&](const InstructionT *Token, const InstructionT *User,
                        SmallVectorImpl<const InstructionT *> &LiveTokens) {
    Check(llvm::is_contained(LiveTokens, Token),
          "Convergence region is not well-nested.",
          {Context.print(Token), Context.print(User)});
    while (LiveTokens.back() != Token)
      LiveTokens.pop_back();

    // Check static rules about cycles.
    auto *BB = User->getParent();
    auto *BBCycle = CI.getCycle(BB);
    if (!BBCycle)
      return;

    auto *DefBB = Token->getParent();
    if (DefBB == BB || BBCycle->contains(DefBB)) {
      // degenerate occurrence of a loop intrinsic
      return;
    }

    Check(ContextT::getIntrinsicID(*User) ==
              Intrinsic::experimental_convergence_loop,
          "Convergence token used by an instruction other than "
          "llvm.experimental.convergence.loop in a cycle that does "
          "not contain the token's definition.",
          {Context.print(User), CI.print(BBCycle)});

    while (true) {
      auto *Parent = BBCycle->getParentCycle();
      if (!Parent || Parent->contains(DefBB))
        break;
      BBCycle = Parent;
    };

    Check(BBCycle->isReducible() && BB == BBCycle->getHeader(),
          "Cycle heart must dominate all blocks in the cycle.",
          {Context.print(User), Context.printAsOperand(BB), CI.print(BBCycle)});
    Check(!CycleHearts.count(BBCycle),
          "Two static convergence token uses in a cycle that does "
          "not contain either token's definition.",
          {Context.print(User), Context.print(CycleHearts[BBCycle]),
           CI.print(BBCycle)});
    CycleHearts[BBCycle] = User;
  };

  ReversePostOrderTraversal<const FunctionT *> RPOT(&F);
  SmallVector<const InstructionT *, 8> LiveTokens;
  for (auto *BB : RPOT) {
    LiveTokens.clear();
    auto LTIt = LiveTokenMap.find(BB);
    if (LTIt != LiveTokenMap.end()) {
      LiveTokens = std::move(LTIt->second);
      LiveTokenMap.erase(LTIt);
    }

    for (auto &I : *BB) {
      if (auto *Token = Tokens.lookup(&I))
        checkToken(Token, &I, LiveTokens);
      if (isConvergenceControlIntrinsic(ContextT::getIntrinsicID(I)))
        LiveTokens.push_back(&I);
    }

    // Propagate token liveness
    for (auto *Succ : successors(BB)) {
      auto *SuccNode = DT.getNode(Succ);
      auto LTIt = LiveTokenMap.find(Succ);
      if (LTIt == LiveTokenMap.end()) {
        // We're the first predecessor: all tokens which dominate the
        // successor are live for now.
        LTIt = LiveTokenMap.try_emplace(Succ).first;
        for (auto LiveToken : LiveTokens) {
          if (!DT.dominates(DT.getNode(LiveToken->getParent()), SuccNode))
            break;
          LTIt->second.push_back(LiveToken);
        }
      } else {
        // Compute the intersection of live tokens.
        auto It = llvm::partition(
            LTIt->second, [&LiveTokens](const InstructionT *Token) {
              return llvm::is_contained(LiveTokens, Token);
            });
        LTIt->second.erase(It, LTIt->second.end());
      }
    }
  }
}

} // end namespace llvm

#endif // LLVM_IR_GENERICCONVERGENCEVERIFIERIMPL_H

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif