summaryrefslogtreecommitdiff
path: root/backend/src/llvm/llvm_barrier_nodup.cpp
blob: b8ffdf4126c602271894c79cd1759c3761c188ad (plain)
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
/*
 * Copyright © 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file llvm_barrier_nodup.cpp
 *
 *  This pass is to remove or add noduplicate function attribute for barrier functions.
 *  Basically, we want to set NoDuplicate for those __gen_barrier_xxx functions. But if
 *  a sub function calls those barrier functions, the sub function will not be inlined
 *  in llvm's inlining pass. This is what we don't want. As inlining such a function in
 *  the caller is safe, we just don't want it to duplicate the call. So Introduce this
 *  pass to remove the NoDuplicate function attribute before the inlining pass and restore
 *  it after.
 *  
 */

#include "llvm_includes.hpp"

#include "llvm/llvm_gen_backend.hpp"
#include "sys/map.hpp"


using namespace llvm;

namespace gbe {
    class BarrierNodup : public ModulePass
    {
    public:
      static char ID;
      BarrierNodup(bool nodup) :
        ModulePass(ID), nodup(nodup) {}

      void getAnalysisUsage(AnalysisUsage &AU) const {

      }

#if LLVM_VERSION_MAJOR * 10 + LLVM_VERSION_MINOR >= 40
      virtual StringRef getPassName() const
#else
      virtual const char *getPassName() const
#endif
      {
        return "SPIR backend: set barrier no duplicate attr";
      }

      virtual bool runOnModule(Module &M)
      {
        using namespace llvm;
        bool changed = false;
        for (auto &F : M) {
          if (F.getName() == "__gen_ocl_barrier_local_and_global" ||
              F.getName() == "__gen_ocl_barrier_local"            ||
              F.getName() == "__gen_ocl_barrier_global") {
            if (nodup) {
              if (!F.hasFnAttribute(Attribute::NoDuplicate)) {
                F.addFnAttr(Attribute::NoDuplicate);
                changed = true;
              }
            } else {
              if (F.hasFnAttribute(Attribute::NoDuplicate)) {
                auto attrs = F.getAttributes();
                F.setAttributes(attrs.removeAttribute(M.getContext(),
#if LLVM_VERSION_MAJOR * 10 + LLVM_VERSION_MINOR >= 50
                                AttributeList::FunctionIndex,
#else
                                AttributeSet::FunctionIndex,
#endif
                                Attribute::NoDuplicate));
                changed = true;
              }
            }
          }
        }

        return changed;
      }
    private:
      bool nodup;
    };


    ModulePass *createBarrierNodupPass(bool Nodup) {
      return new BarrierNodup(Nodup);
    }

    char BarrierNodup::ID = 0;
} // end namespace