1 /* Copyright 2018 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 <memory>
17 #include <utility>
18
19 #include "absl/memory/memory.h"
20 #include "tensorflow/compiler/xla/literal.h"
21 #include "tensorflow/compiler/xla/literal_util.h"
22 #include "tensorflow/compiler/xla/service/gpu/tests/gpu_codegen_test.h"
23 #include "tensorflow/compiler/xla/service/hlo_computation.h"
24 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
25 #include "tensorflow/compiler/xla/service/hlo_module.h"
26 #include "tensorflow/compiler/xla/service/hlo_opcode.h"
27 #include "tensorflow/core/platform/test.h"
28
29 namespace xla {
30 namespace gpu {
31
32 class GpuCopyTest : public GpuCodegenTest {};
33
34 // The GPU backend should not emit a copy kernel for the kCopy instruction in
35 // this test. Instead, it should generate a CopyThunk which invokes cuMemcpy at
36 // runtime.
TEST_F(GpuCopyTest,UseMemcpy)37 TEST_F(GpuCopyTest, UseMemcpy) {
38 HloComputation::Builder builder(TestName());
39
40 Literal literal = LiteralUtil::CreateR2<float>({{1.0, 2.0}, {3.0, 4.0}});
41 HloInstruction* constant = builder.AddInstruction(
42 HloInstruction::CreateConstant(std::move(literal)));
43 builder.AddInstruction(HloInstruction::CreateUnary(
44 constant->shape(), HloOpcode::kCopy, constant));
45
46 std::unique_ptr<HloComputation> computation = builder.Build();
47
48 auto hlo_module = CreateNewVerifiedModule();
49 hlo_module->AddEntryComputation(std::move(computation));
50
51 // There should not be any kernel prefixed "copy".
52 CompileAndVerifyIr(std::move(hlo_module), "; CHECK-NOT: define void @_copy",
53 /*match_optimized_ir=*/false);
54 }
55
56 } // namespace gpu
57 } // namespace xla
58