tvm
Loading...
Searching...
No Matches
intrin_rule.h
Go to the documentation of this file.
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
24#ifndef TVM_CODEGEN_INTRIN_RULE_H_
25#define TVM_CODEGEN_INTRIN_RULE_H_
26
27#include <tvm/ffi/container/array.h>
28#include <tvm/ffi/dtype.h>
29#include <tvm/ir/expr.h>
30#include <tvm/ir/op.h>
31#include <tvm/runtime/base.h>
32#include <tvm/target/target.h>
33#include <tvm/tirx/op/abi.h>
34
35#include <string>
36
37namespace tvm {
38namespace codegen {
39namespace intrin {
40
43 std::string operator()(const PrimType& ty, std::string name) const {
44 if (ty->dtype == DLDataType{kDLFloat, 32, 1}) {
45 return name + 'f';
46 } else if (ty->dtype == DLDataType{kDLFloat, 64, 1}) {
47 return name;
48 } else {
49 return "";
50 }
51 }
52};
53
55struct Direct {
56 std::string operator()(const PrimType& ty, std::string name) const { return name; }
57};
58
73template <typename T, bool dtype_from_arg = false>
74inline PrimExpr DispatchPureExtern(const PrimExpr& e, const Target& target) {
75 const CallObj* call = e.as<CallObj>();
76 TVM_FFI_ICHECK(call != nullptr);
77 // Primitive intrinsic names use the prim. namespace prefix.
78 const OpObj* op = call->op.as<OpObj>();
79 TVM_FFI_ICHECK(op != nullptr);
80 std::string name = op->name;
81 TVM_FFI_ICHECK(name.substr(0, 5) == "prim.") << "Unexpected intrinsic name: " << name;
82 if (dtype_from_arg) {
83 TVM_FFI_ICHECK_EQ(call->args.size(), 1U);
84 }
85 PrimType type = dtype_from_arg ? call->args[0].as_or_throw<PrimExpr>().ty()
86 : call->ty.as_or_throw<PrimType>();
87 name = T()(type, name.substr(5));
88
89 if (!name.empty()) {
90 ffi::Array<Expr> new_args = {StringImm(name)};
91 for (const PrimExpr& arg : call->args.as_or_throw<ffi::Array<PrimExpr>>()) {
92 new_args.push_back(arg);
93 }
94 return Call(e.ty(), tirx::call_pure_extern_op(), new_args).as_or_throw<PrimExpr>();
95 } else {
96 return e;
97 }
98}
99
106
113
114} // namespace intrin
115} // namespace codegen
116} // namespace tvm
117#endif // TVM_CODEGEN_INTRIN_RULE_H_
Base expr nodes in TVM.
Canonical primitive operators and their registration.
#define TVM_TIRX_DLL
Definition base.h:55
Abi operations for TIRx.
Call corresponds to callable invocation.
Definition expr.h:788
Expr op
The operator/function being invoked.
Definition expr.h:799
ffi::Array< Expr > args
The arguments of the call.
Definition expr.h:802
Managed reference to CallObj.
Definition expr.h:825
Type ty
The deduced or annotated type of the expression.
Definition expr.h:80
Metadata for a canonical primitive operator invoked through Call.
Definition op.h:77
ffi::String name
Canonical operator name.
Definition op.h:84
Typed reference/view over any Expr whose ExprObj::ty is PrimType.
Definition expr.h:187
Definition type.h:182
Managed reference to a string literal.
Definition expr.h:278
Managed reference class to TargetObj.
Definition target.h:155
ExpectedType ty() const
Definition expr.h:164
PrimExpr DispatchPureExtern(const PrimExpr &e, const Target &target)
Dispatch pure extern function.
Definition intrin_rule.h:74
TVM_TIRX_DLL PrimExpr DispatchFastErf(const PrimExpr &e, const Target &target)
Lower a unary float16/float32 erf call to a rational approximation. The expansion clamps the input to...
TVM_TIRX_DLL PrimExpr DispatchNumericallyStableTanh(const PrimExpr &e, const Target &target)
Lower a unary floating-point tanh call using sign-dependent exponential formulas. The expansion avoid...
TVM_TIRX_DLL const Op & call_pure_extern_op()
Call a pure external C function with types supplied by the caller.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
Preserve the intrinsic name for every primitive type.
Definition intrin_rule.h:55
std::string operator()(const PrimType &ty, std::string name) const
Definition intrin_rule.h:56
Append f for scalar float32, preserve scalar float64, and reject other types.
Definition intrin_rule.h:42
std::string operator()(const PrimType &ty, std::string name) const
Definition intrin_rule.h:43
Compilation target object.