• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2022 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/mlir/transforms/runtime/calling_convention.h"
17 
18 #include <iterator>
19 #include <utility>
20 
21 #include "tensorflow/compiler/xla/mlir/ir/runtime/rt_ops.h"
22 
23 namespace xla {
24 namespace runtime {
25 
DefaultCallingConvention()26 CallingConvention DefaultCallingConvention() {
27   return [](mlir::FunctionType func) {
28     mlir::MLIRContext* ctx = func.getContext();
29 
30     llvm::SmallVector<mlir::Type> inputs = {KernelContextType::get(ctx)};
31     inputs.reserve(1 + func.getNumInputs());
32     llvm::append_range(inputs, func.getInputs());
33 
34     return mlir::FunctionType::get(ctx, inputs, func.getResults());
35   };
36 }
37 
DefaultCallingConvention(mlir::TypeConverter type_converter)38 CallingConvention DefaultCallingConvention(mlir::TypeConverter type_converter) {
39   return [c = std::move(type_converter)](mlir::FunctionType func) mutable {
40     mlir::MLIRContext* ctx = func.getContext();
41 
42     // Track if all type conversions were successful.
43     bool failed_conversion = false;
44     auto convert = [&](mlir::Type type) -> mlir::Type {
45       auto converted = c.convertType(type);
46       if (!converted) failed_conversion = true;
47       return converted;
48     };
49 
50     // Add kernel context as the first argument.
51     llvm::SmallVector<mlir::Type> inputs = {KernelContextType::get(ctx)};
52     inputs.reserve(1 + func.getNumInputs());
53     llvm::transform(func.getInputs(), std::back_inserter(inputs), convert);
54 
55     // Apply type conversion to all results types.
56     llvm::SmallVector<mlir::Type> results;
57     results.reserve(func.getNumResults());
58     llvm::transform(func.getResults(), std::back_inserter(results), convert);
59 
60     // Return null if any of the type conversions failed.
61     if (failed_conversion) return mlir::FunctionType();
62 
63     return mlir::FunctionType::get(ctx, inputs, results);
64   };
65 }
66 
ResultsToOutsCallingConvention(mlir::TypeConverter type_converter)67 CallingConvention ResultsToOutsCallingConvention(
68     mlir::TypeConverter type_converter) {
69   return [c = std::move(type_converter)](mlir::FunctionType func) mutable {
70     mlir::MLIRContext* ctx = func.getContext();
71 
72     // Track if all type conversions were successful.
73     bool failed_conversion = false;
74 
75     auto convert = [&](mlir::Type type) -> mlir::Type {
76       auto converted = c.convertType(type);
77       if (!converted) failed_conversion = true;
78       return converted;
79     };
80 
81     llvm::SmallVector<mlir::Type> inputs;
82     inputs.reserve(1 + func.getNumInputs() + func.getNumResults());
83     inputs.push_back(KernelContextType::get(ctx));
84     llvm::transform(func.getInputs(), std::back_inserter(inputs), convert);
85     llvm::transform(func.getResults(), std::back_inserter(inputs), convert);
86 
87     // Return null if any of the type conversions failed.
88     if (failed_conversion) return mlir::FunctionType();
89 
90     return mlir::FunctionType::get(ctx, inputs, {});
91   };
92 }
93 
94 }  // namespace runtime
95 }  // namespace xla
96