File: WorkitemHandlerChooser.cc

package info (click to toggle)
pocl 6.0-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 25,304 kB
  • sloc: lisp: 149,513; ansic: 103,778; cpp: 54,947; python: 1,513; sh: 949; ruby: 255; pascal: 226; tcl: 180; makefile: 173; java: 72; xml: 49
file content (147 lines) | stat: -rw-r--r-- 4,984 bytes parent folder | download | duplicates (2)
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
// LLVM function pass to select the best way to create a work group
// function for a kernel and work group size.
//
// Copyright (c) 2012-2019 Pekka Jääskeläinen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.


#include "CompilerWarnings.h"
IGNORE_COMPILER_WARNING("-Wmaybe-uninitialized")
#include <llvm/ADT/Twine.h>
POP_COMPILER_DIAGS
IGNORE_COMPILER_WARNING("-Wunused-parameter")
#include "llvm/Analysis/PostDominators.h"
#include "llvm/Analysis/LoopInfo.h"

#include "CanonicalizeBarriers.h"
#include "Kernel.h"
#include "LLVMUtils.h"
#include "Workgroup.h"
#include "WorkitemHandlerChooser.h"
#include "WorkitemLoops.h"
#include "WorkitemReplication.h"
POP_COMPILER_DIAGS

#include "pocl_llvm_api.h"

#include <iostream>

#define DEBUG_TYPE "workitem-loops"

#define PASS_NAME "workitem-handler-chooser"
#define PASS_CLASS pocl::WorkitemHandlerChooser
#define PASS_DESC                                                              \
  "Finds the best way to handle work-items to produce a multi-WG function."

namespace pocl {

using namespace llvm;

static WorkitemHandlerType runWorkitemHandlerChooser(Function &F) {
  if (!isKernelToProcess(F))
    return WorkitemHandlerType::INVALID;

  WorkitemHandlerType Result = WorkitemHandlerType::INVALID;
  unsigned long WGLocalSizeX = 0;
  unsigned long WGLocalSizeY = 0;
  unsigned long WGLocalSizeZ = 0;
  bool WGDynamicLocalSize = false;

  getModuleIntMetadata(*F.getParent(), "WGLocalSizeX", WGLocalSizeX);
  getModuleIntMetadata(*F.getParent(), "WGLocalSizeY", WGLocalSizeY);
  getModuleIntMetadata(*F.getParent(), "WGLocalSizeZ", WGLocalSizeZ);
  getModuleBoolMetadata(*F.getParent(), "WGDynamicLocalSize",
                        WGDynamicLocalSize);

  std::string method = "auto";
  if (getenv("POCL_WORK_GROUP_METHOD") != NULL)
    {
      method = getenv("POCL_WORK_GROUP_METHOD");
      if ((method == "repl" || method == "workitemrepl") && !WGDynamicLocalSize)
        Result = WorkitemHandlerType::FULL_REPLICATION;
      else if (method == "loops" || method == "workitemloops" || method == "loopvec")
        Result = WorkitemHandlerType::LOOPS;
      else if (method == "cbs")
        Result = WorkitemHandlerType::CBS;
      else if (method != "auto")
        {
          std::cerr << "Unknown work group generation method. Using 'auto'." << std::endl;
          method = "auto";
        }
    }

  if (method == "auto") 
    {
      unsigned ReplThreshold = 2;
      if (getenv("POCL_FULL_REPLICATION_THRESHOLD") != NULL) 
      {
        ReplThreshold = atoi(getenv("POCL_FULL_REPLICATION_THRESHOLD"));
      }

      if (!WGDynamicLocalSize &&
          WGLocalSizeX * WGLocalSizeY * WGLocalSizeZ <= ReplThreshold) {
        Result = WorkitemHandlerType::FULL_REPLICATION;
      } else {
        Result = WorkitemHandlerType::LOOPS;
      }
    }
    return Result;
}

/**********************************************************************/

#if LLVM_MAJOR < MIN_LLVM_NEW_PASSMANAGER
char WorkitemHandlerChooser::ID = 0;

bool WorkitemHandlerChooser::runOnFunction(Function &F) {
  chosenHandler_.WIH = runWorkitemHandlerChooser(F);
  return false;
}

void WorkitemHandlerChooser::getAnalysisUsage(AnalysisUsage &AU) const {
  AU.setPreservesAll();
}

REGISTER_OLD_FPASS(PASS_NAME, PASS_CLASS, PASS_DESC);

#else

llvm::AnalysisKey WorkitemHandlerChooser::Key;

WorkitemHandlerResult
WorkitemHandlerChooser::run(llvm::Function &F,
                            llvm::FunctionAnalysisManager &AM) {
  return runWorkitemHandlerChooser(F);
}

bool WorkitemHandlerResult::invalidate(
    llvm::Function &F, const llvm::PreservedAnalyses PA,
    llvm::AnalysisManager<llvm::Function>::Invalidator &Inv) {
  // Check whether the analysis has been explicitly invalidated.
  // Otherwise, it stays valid
  auto PAC = PA.getChecker<WorkitemHandlerChooser>();
  return !PAC.preservedWhenStateless();
}

REGISTER_NEW_FANALYSIS(PASS_NAME, PASS_CLASS, PASS_DESC);

#endif

} // namespace pocl