• 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 BufferValue* 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   return SanitizeConstantName(instr.name());
47 }
48 
SanitizeConstantName(absl::string_view name)49 string SanitizeConstantName(absl::string_view name) {
50   std::string instr_name(name);
51   for (char& c : instr_name) {
52     // Having a hyphen or a dot in a global variable name can crash the LLVM PTX
53     // backend.
54     if (c == '.' || c == '-') {
55       c = '_';
56     }
57   }
58   return instr_name;
59 }
60 
ConstantHloToGlobalName(const HloInstruction & instr)61 string ConstantHloToGlobalName(const HloInstruction& instr) {
62   return ConstantNameToGlobalName(instr.name());
63 }
64 
ConstantNameToGlobalName(absl::string_view name)65 string ConstantNameToGlobalName(absl::string_view name) {
66   // Check that names are sanitized and stored in the HLO instructions
67   // before constant buffer allocation.
68   DCHECK_EQ(name, SanitizeConstantName(name));
69   return absl::StrCat("buffer_for_", name);
70 }
71 
ConstantBufferAllocationToGlobalName(const BufferAllocation & allocation)72 string ConstantBufferAllocationToGlobalName(
73     const BufferAllocation& allocation) {
74   return ConstantNameToGlobalName(
75       SanitizeConstantName(InstrForConstantBufferAllocation(allocation)));
76 }
77 
LiteralForConstantAllocation(const BufferAllocation & allocation)78 const Literal& LiteralForConstantAllocation(
79     const BufferAllocation& allocation) {
80   return InstrForConstantBufferAllocation(allocation).literal();
81 }
82 }  // namespace llvm_ir
83 }  // namespace xla
84