• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
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 "target_machine_builder.h"
17 #include "transforms/transform_utils.h"
18 
19 #include <llvm/MC/TargetRegistry.h>
20 
21 namespace ark::llvmbackend {
22 
Build()23 llvm::Expected<std::unique_ptr<llvm::TargetMachine>> TargetMachineBuilder::Build()
24 {
25     ASSERT(triple_.getArch() != llvm::Triple::UnknownArch);
26     std::string error;
27     auto target = llvm::TargetRegistry::lookupTarget("", triple_, error);
28     if (!error.empty()) {
29         return llvm::createStringError(llvm::inconvertibleErrorCode(),
30                                        llvm::StringRef {"Could not lookupTarget by triple = '"} + triple_.str() +
31                                            "', error = '" + error + "'");
32     }
33     ASSERT(target != nullptr);
34 
35     llvm::TargetOptions targetOptions {};
36     auto machine = target->createTargetMachine(triple_.getTriple(), cpu_, features_, targetOptions, llvm::Reloc::PIC_,
37                                                llvm::CodeModel::Small, optlevel_);
38     ASSERT(machine != nullptr);
39     return std::unique_ptr<llvm::TargetMachine>(machine);
40 }
41 
SetFeatures(const std::vector<std::string> & features)42 TargetMachineBuilder &TargetMachineBuilder::SetFeatures(const std::vector<std::string> &features)
43 {
44     features_ = llvm::join(features.cbegin(), features.cend(), ",");
45     return *this;
46 }
47 }  // namespace ark::llvmbackend
48