• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 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/buffer_assignment_util.h"
17 #include "absl/strings/str_cat.h"
18 
19 namespace xla {
20 namespace llvm_ir {
InstrForConstantBufferAllocation(const BufferAllocation & allocation)21 static const HloInstruction& InstrForConstantBufferAllocation(
22     const BufferAllocation& allocation) {
23   CHECK(allocation.is_constant());
24   HloInstruction* const_instr = nullptr;
25   for (const auto& buffer_offset_pair : allocation.assigned_buffers()) {
26     const LogicalBuffer* buffer = buffer_offset_pair.first;
27     // BufferAssignment may have assigned non-constant instructions to this
28     // allocation too so we can't CHECK this condition.  E.g. for
29     //
30     //   while(init = constant, body = identity, cond = ...)
31     //
32     // the LogicalBuffer for the kWhile instruction will have the same
33     // BufferAllocation as the LogicalBuffer for the (init) constant.
34     if (buffer->instruction()->opcode() == HloOpcode::kConstant) {
35       CHECK_EQ(const_instr, nullptr)
36           << const_instr->ToString() << " " << buffer->ToString();
37       const_instr = buffer->instruction();
38     }
39   }
40   CHECK_NE(const_instr, nullptr);
41   return *const_instr;
42 }
43 
SanitizeConstantName(const HloInstruction & instr)44 string SanitizeConstantName(const HloInstruction& instr) {
45   CHECK_EQ(instr.opcode(), HloOpcode::kConstant);
46   string instr_name = instr.name();
47   for (char& c : instr_name) {
48     // Having a hyphen or a dot in a global variable name can crash the LLVM PTX
49     // backend.
50     if (c == '.' || c == '-') {
51       c = '_';
52     }
53   }
54   return instr_name;
55 }
56 
ConstantBufferAllocationToGlobalName(const BufferAllocation & allocation)57 string ConstantBufferAllocationToGlobalName(
58     const BufferAllocation& allocation) {
59   const HloInstruction& instr = InstrForConstantBufferAllocation(allocation);
60   string instr_name = instr.name();
61   // Check that names are sanitized and stored in the HLO instructions
62   // before constant buffer allocation.
63   DCHECK_EQ(instr_name, SanitizeConstantName(instr));
64   return absl::StrCat("buffer_for_", instr_name);
65 }
66 
LiteralForConstantAllocation(const BufferAllocation & allocation)67 const Literal& LiteralForConstantAllocation(
68     const BufferAllocation& allocation) {
69   return InstrForConstantBufferAllocation(allocation).literal();
70 }
71 }  // namespace llvm_ir
72 }  // namespace xla
73