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#include <sstream>
20#include <string>
21
22#include "../../../../target/source/codegen_c_host.h"
23
24namespace tvm {
25namespace relay {
26namespace contrib {
27namespace example_target_hooks {
28
29using namespace tir;
30
31class CodeGenExampleTargetHook : public codegen::CodeGenCHost {
32 public:
33 using codegen::CodeGenCHost::VisitExpr_;
34
35 /*!
36 * \brief Emit code that changes adds to multiplies for testing
37 */
38 void VisitExpr_(const SubNode* op, std::ostream& os) final {
39 os << '(';
40 PrintExpr(op->a, os);
41 os << " * ";
42 PrintExpr(op->b, os);
43 os << ')';
44 }
45};
46
47runtime::Module TIRToRuntime(IRModule mod, Target target) {
48 bool output_ssa = false;
49 bool emit_asserts = false;
50 bool emit_fwd_func_decl = false;
51 CodeGenExampleTargetHook codegen;
52 Array<String> function_names;
53 std::unordered_set<std::string> devices;
54 codegen.Init(output_ssa, emit_asserts, emit_fwd_func_decl, target->str(), devices);
55 for (auto kv : mod->functions) {
56 auto prim_func = Downcast<PrimFunc>(kv.second);
57 auto global_symbol = prim_func->GetAttr<String>(tvm::attr::kGlobalSymbol);
58 function_names.push_back(global_symbol.value());
59 codegen.AddFunction(prim_func);
60 }
61 std::string code = codegen.Finish();
62 return codegen::CSourceModuleCreate(code, "c", function_names);
63}
64
65} // namespace example_target_hooks
66} // namespace contrib
67} // namespace relay
68} // namespace tvm
69