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
18 #include <algorithm>
19
20 #include "absl/strings/str_cat.h"
21
22 namespace xla {
23 namespace llvm_ir {
InstrForConstantBufferAllocation(const BufferAllocation & allocation)24 static const HloInstruction& InstrForConstantBufferAllocation(
25 const BufferAllocation& allocation) {
26 CHECK(allocation.is_constant());
27 HloInstruction* const_instr = nullptr;
28 for (const auto& buffer_offset_pair : allocation.assigned_buffers()) {
29 const BufferValue* buffer = buffer_offset_pair.first;
30 // BufferAssignment may have assigned non-constant instructions to this
31 // allocation too so we can't CHECK this condition. E.g. for
32 //
33 // while(init = constant, body = identity, cond = ...)
34 //
35 // the LogicalBuffer for the kWhile instruction will have the same
36 // BufferAllocation as the LogicalBuffer for the (init) constant.
37 if (buffer->instruction()->opcode() == HloOpcode::kConstant) {
38 CHECK_EQ(const_instr, nullptr)
39 << const_instr->ToString() << " " << buffer->ToString();
40 const_instr = buffer->instruction();
41 }
42 }
43 CHECK_NE(const_instr, nullptr);
44 return *const_instr;
45 }
46
SanitizeConstantName(const HloInstruction & instr)47 std::string SanitizeConstantName(const HloInstruction& instr) {
48 CHECK_EQ(instr.opcode(), HloOpcode::kConstant);
49 return SanitizeConstantName(instr.name());
50 }
51
SanitizeConstantName(absl::string_view name)52 std::string SanitizeConstantName(absl::string_view name) {
53 std::string instr_name(name);
54 // Replace characters which would require the identifier to be quoted and
55 // would therefore crash the LLVM PTX backend.
56 std::replace_if(
57 instr_name.begin(), instr_name.end(),
58 [](char c) { return c == '.' || c == '-' || c == ';'; }, '_');
59 return instr_name;
60 }
61
ConstantHloToGlobalName(const HloInstruction & instr)62 std::string ConstantHloToGlobalName(const HloInstruction& instr) {
63 return ConstantNameToGlobalName(instr.name());
64 }
65
ConstantNameToGlobalName(absl::string_view name)66 std::string ConstantNameToGlobalName(absl::string_view name) {
67 // Check that names are sanitized and stored in the HLO instructions
68 // before constant buffer allocation.
69 DCHECK_EQ(name, SanitizeConstantName(name));
70 return absl::StrCat("buffer_for_", name);
71 }
72
ConstantBufferAllocationToGlobalName(const BufferAllocation & allocation)73 std::string ConstantBufferAllocationToGlobalName(
74 const BufferAllocation& allocation) {
75 return ConstantNameToGlobalName(
76 SanitizeConstantName(InstrForConstantBufferAllocation(allocation)));
77 }
78
LiteralForConstantAllocation(const BufferAllocation & allocation)79 const Literal& LiteralForConstantAllocation(
80 const BufferAllocation& allocation) {
81 return InstrForConstantBufferAllocation(allocation).literal();
82 }
83 } // namespace llvm_ir
84 } // namespace xla
85