• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/compiler/xla/service/llvm_ir/alias_analysis.h"
17 
18 #include <map>
19 
20 #include "llvm/IR/MDBuilder.h"
21 #include "tensorflow/compiler/xla/service/llvm_ir/llvm_util.h"
22 #include "tensorflow/compiler/xla/service/logical_buffer.h"
23 #include "tensorflow/compiler/xla/types.h"
24 
25 namespace xla {
26 namespace llvm_ir {
27 
28 // Sentry allocation used to represent parameters of the entry computation in
29 // alias_scope_metadata_ and noalias_metadata_.
30 static const BufferAllocation* kParameterAllocation = new BufferAllocation(
31     /*index=*/-1, /*size=*/0, LogicalBuffer::Color(0));
32 
AddAliasingInformationToIrArray(const HloInstruction & hlo,llvm_ir::IrArray * array,const ShapeIndex & index)33 void AliasAnalysis::AddAliasingInformationToIrArray(const HloInstruction& hlo,
34                                                     llvm_ir::IrArray* array,
35                                                     const ShapeIndex& index) {
36   BufferAllocation::Slice buffer_slice;
37   if (hlo.opcode() == HloOpcode::kParameter &&
38       hlo.parent() == hlo.parent()->parent()->entry_computation()) {
39     // Entry computation parameters may alias with each other but may not alias
40     // with our temporary buffers.
41     buffer_slice = BufferAllocation::Slice(kParameterAllocation, 0, 0);
42   } else {
43     const std::set<BufferAllocation::Slice> slices =
44         assignment_.GetAllSlices(&hlo, index);
45     if (slices.empty() || slices.size() > 1) {
46       // Skip HLOs which don't have a buffer assigned or for which the
47       // buffer can't be determined statically. We cannot determine their
48       // aliasing properties in these cases.
49       return;
50     }
51     buffer_slice = *slices.begin();
52   }
53 
54   if (module_.config().debug_options().xla_llvm_enable_alias_scope_metadata()) {
55     llvm::MDNode*& alias_scope_md = alias_scope_metadata_[buffer_slice];
56     if (alias_scope_md == nullptr) {
57       alias_scope_md =
58           GetAliasScopeMetadataForBuffer(buffer_slice, GetAliasDomain());
59     }
60     if (alias_scope_md != nullptr) {
61       array->AddAliasScopeMetadata(alias_scope_md);
62     }
63   }
64 
65   if (module_.config().debug_options().xla_llvm_enable_noalias_metadata()) {
66     llvm::MDNode*& noalias_md = noalias_metadata_[buffer_slice];
67     if (noalias_md == nullptr) {
68       noalias_md = GetNoaliasMetadataForBuffer(buffer_slice, GetAliasDomain(),
69                                                assignment_, hlo);
70     }
71     if (noalias_md != nullptr) {
72       array->AddNoaliasMetadata(noalias_md);
73     }
74   }
75 
76   if (module_.config()
77           .debug_options()
78           .xla_llvm_enable_invariant_load_metadata()) {
79     // Parameters of the entry computation are never stored to, loading from a
80     // parameter pointer should always return the same result within a loop.
81     if (hlo.opcode() == HloOpcode::kParameter) {
82       const std::vector<HloInstruction*>& parameter_instructions =
83           module_.entry_computation()->parameter_instructions();
84       if (absl::c_linear_search(parameter_instructions, &hlo)) {
85         array->MarkInvariantOverWholeProgram(context_);
86       }
87     }
88   }
89 }
90 
GetAliasDomain()91 llvm::MDNode* AliasAnalysis::GetAliasDomain() {
92   llvm::MDBuilder metadata_builder(*context_);
93   if (alias_domain_ == nullptr) {
94     // We use createAliasScopeDomain rather than createAnonymousAliasScopeDomain
95     // so that when functions get inlined, we continue using the one domain,
96     // rather than duplicating it (and thus having two AA domains in one
97     // function).
98     //
99     // A side-effect of this is that if you ever compile two HLO modules in the
100     // same LLVM module, they'll have the same alias scope domain.  This isn't a
101     // problem because the two HLO modules will never interact with one another.
102     alias_domain_ =
103         metadata_builder.createAliasScopeDomain("XLA global AA domain");
104   }
105   return alias_domain_;
106 }
107 
GetAliasScopeMetadataForBuffer(const BufferAllocation::Slice & buffer_slice,llvm::MDNode * domain)108 llvm::MDNode* AliasAnalysis::GetAliasScopeMetadataForBuffer(
109     const BufferAllocation::Slice& buffer_slice, llvm::MDNode* domain) {
110   // While we could synthesize an alias.scope, doing so is not more profitable
111   // than LLVM's default behavior.
112   if (buffer_slice.allocation() == kParameterAllocation) {
113     return nullptr;
114   }
115 
116   llvm::MDBuilder metadata_builder(domain->getContext());
117   llvm::MDNode* scope = metadata_builder.createAliasScope(
118       "buffer: " + buffer_slice.ToString(), domain);
119   llvm::MDNode* scope_list = llvm::MDNode::get(domain->getContext(), scope);
120   return scope_list;
121 }
122 
GetNoaliasMetadataForBuffer(const BufferAllocation::Slice & buffer_slice,llvm::MDNode * domain,const BufferAssignment & assignment,const HloInstruction & hlo)123 llvm::MDNode* AliasAnalysis::GetNoaliasMetadataForBuffer(
124     const BufferAllocation::Slice& buffer_slice, llvm::MDNode* domain,
125     const BufferAssignment& assignment, const HloInstruction& hlo) {
126   // We want to construct a list of buffers which:
127   //
128   // 1. Do not alias the given buffer.
129   // 2. Will plausibly be used in the vicinity of the given buffer.
130   //
131   // Making the noalias set overly large will result in either a massive
132   // slowdown in LLVM or LLVM will just ignore the noalias set.
133   //
134   // A plausible list of instructions are:
135   // 1. Users of the given hlo.
136   // 2. Operands of users of the given hlo.
137   // 3. Operands of the given hlo.
138   //
139   // This set can be increased as we need.
140   std::vector<const LogicalBuffer*> worklist;
141   auto add_buffers_to_worklist =
142       [&worklist, &assignment](const HloInstruction* instruction) {
143         ShapeUtil::ForEachSubshape(
144             instruction->shape(),
145             [&](const Shape& /*shape*/, const ShapeIndex& index) {
146               for (const LogicalBuffer* buffer :
147                    assignment.GetSourceBuffers(instruction, index)) {
148                 worklist.push_back(buffer);
149               }
150             });
151       };
152 
153   for (HloInstruction* user : hlo.users()) {
154     add_buffers_to_worklist(user);
155     for (HloInstruction* operand : user->operands()) {
156       add_buffers_to_worklist(operand);
157     }
158   }
159 
160   add_buffers_to_worklist(&hlo);
161   for (HloInstruction* operand : hlo.operands()) {
162     add_buffers_to_worklist(operand);
163   }
164 
165   std::set<BufferAllocation::Slice> buffers;
166   for (const LogicalBuffer* buffer : worklist) {
167     // Skip buffers which cannot be added to the noalias set.
168     if (!assignment.HasAllocation(*buffer) ||
169         buffer->instruction()->opcode() == HloOpcode::kParameter) {
170       continue;
171     }
172     const BufferAllocation::Slice noalias_slice =
173         assignment.GetAssignedAllocation(*buffer).GetSlice(*buffer);
174     // Our buffer must not overlap with the noalias slice.
175     if (!buffer_slice.OverlapsWith(noalias_slice)) {
176       buffers.insert(noalias_slice);
177       // Some instructions have too many operands, causing the noalias set to be
178       // too large. To reduce compilation time (b/31901575), truncate noalias
179       // sets to at most 500 elements.
180       //
181       // Future work: improvements to LLVM's scoped AA that avoid creating a
182       // MDNode set for every alias query can help to reduce the compilation
183       // time as well.
184       constexpr int kMaxNoAliasSetSize = 500;
185       if (buffers.size() >= kMaxNoAliasSetSize) {
186         break;
187       }
188     }
189   }
190 
191   // Don't bother constructing a noalias metadata node if it would be empty.
192   if (buffers.empty()) {
193     return nullptr;
194   }
195 
196   llvm::MDBuilder metadata_builder(domain->getContext());
197   std::vector<llvm::Metadata*> scopes;
198   for (const BufferAllocation::Slice noalias_slice : buffers) {
199     llvm::MDNode* scope = metadata_builder.createAliasScope(
200         "buffer: " + noalias_slice.ToString(), domain);
201     scopes.push_back(scope);
202   }
203   llvm::MDNode* noalias_list =
204       llvm::MDNode::get(domain->getContext(), AsArrayRef(scopes));
205   return noalias_list;
206 }
207 
208 }  // namespace llvm_ir
209 }  // namespace xla
210