1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/*!
21 * \file intrin_rule.h
22 * \brief Utility to generate intrinsic rules
23 */
24#ifndef TVM_TARGET_INTRIN_RULE_H_
25#define TVM_TARGET_INTRIN_RULE_H_
26
27#include <tvm/runtime/registry.h>
28#include <tvm/tir/builtin.h>
29#include <tvm/tir/expr.h>
30
31#include <string>
32
33namespace tvm {
34namespace codegen {
35namespace intrin {
36using namespace tir;
37
38// Add float suffix to the intrinsics
39struct FloatSuffix {
40 std::string operator()(DataType t, std::string name) const {
41 if (t == DataType::Float(32)) {
42 return name + 'f';
43 } else if (t == DataType::Float(64)) {
44 return name;
45 } else {
46 return "";
47 }
48 }
49};
50
51// Return the intrinsic name
52struct Direct {
53 std::string operator()(DataType t, std::string name) const { return name; }
54};
55
56// Call pure extern function.
57template <typename T>
58inline PrimExpr DispatchPureExtern(const PrimExpr& e) {
59 const CallNode* call = e.as<CallNode>();
60 ICHECK(call != nullptr);
61 // Use string based dispatch to extern for backward compact
62 // TODO(tvm-team) replace once the new dispatching system is inplace.
63 const OpNode* op = call->op.as<OpNode>();
64 ICHECK(op != nullptr);
65 std::string name = op->name;
66 ICHECK_EQ(name.substr(0, 4), "tir.");
67 name = T()(call->dtype, name.substr(4));
68
69 if (name.length() != 0) {
70 Array<PrimExpr> new_args = {StringImm(name)};
71 for (auto arg : call->args) {
72 new_args.push_back(arg);
73 }
74 return Call(call->dtype, builtin::call_pure_extern(), new_args);
75 } else {
76 return e;
77 }
78}
79
80} // namespace intrin
81} // namespace codegen
82} // namespace tvm
83#endif // TVM_TARGET_INTRIN_RULE_H_
84