Skip to content

Commit 1915fa1

Browse files
authored
Utils: Add pass to declare runtime libcalls (#147534)
This will be useful for testing the set of calls for different systems, and eventually the product of context specific modifiers applied. In the future we should also know the type signatures, and be able to emit the correct one.
1 parent 0580563 commit 1915fa1

File tree

7 files changed

+80
-0
lines changed

7 files changed

+80
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===- DeclareRuntimeLibcalls.h ---------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_TRANSFORMS_UTILS_DECLARERUNTIMELIBCALLS_H
10+
#define LLVM_TRANSFORMS_UTILS_DECLARERUNTIMELIBCALLS_H
11+
12+
#include "llvm/IR/PassManager.h"
13+
14+
namespace llvm {
15+
class DeclareRuntimeLibcallsPass
16+
: public PassInfoMixin<DeclareRuntimeLibcallsPass> {
17+
public:
18+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
19+
};
20+
21+
} // end namespace llvm
22+
23+
#endif // LLVM_TRANSFORMS_UTILS_DECLARERUNTIMELIBCALLS_H

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@
339339
#include "llvm/Transforms/Utils/CountVisits.h"
340340
#include "llvm/Transforms/Utils/DXILUpgrade.h"
341341
#include "llvm/Transforms/Utils/Debugify.h"
342+
#include "llvm/Transforms/Utils/DeclareRuntimeLibcalls.h"
342343
#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
343344
#include "llvm/Transforms/Utils/FixIrreducible.h"
344345
#include "llvm/Transforms/Utils/HelloWorld.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ MODULE_PASS("ctx-prof-flatten-prethinlink",
7070
MODULE_PASS("noinline-nonprevailing", NoinlineNonPrevailing())
7171
MODULE_PASS("deadargelim", DeadArgumentEliminationPass())
7272
MODULE_PASS("debugify", NewPMDebugifyPass())
73+
MODULE_PASS("declare-runtime-libcalls", DeclareRuntimeLibcallsPass())
7374
MODULE_PASS("dfsan", DataFlowSanitizerPass())
7475
MODULE_PASS("dot-callgraph", CallGraphDOTPrinterPass())
7576
MODULE_PASS("dxil-upgrade", DXILUpgradePass())

llvm/lib/Transforms/Utils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_llvm_component_library(LLVMTransformUtils
2020
CtorUtils.cpp
2121
CountVisits.cpp
2222
Debugify.cpp
23+
DeclareRuntimeLibcalls.cpp
2324
DemoteRegToStack.cpp
2425
DXILUpgrade.cpp
2526
EntryExitInstrumenter.cpp
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===- DeclareRuntimeLibcalls.cpp -----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Insert declarations for all runtime library calls known for the target.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "llvm/Transforms/Utils/DeclareRuntimeLibcalls.h"
14+
#include "llvm/IR/Module.h"
15+
#include "llvm/IR/RuntimeLibcalls.h"
16+
17+
using namespace llvm;
18+
19+
PreservedAnalyses DeclareRuntimeLibcallsPass::run(Module &M,
20+
ModuleAnalysisManager &MAM) {
21+
RTLIB::RuntimeLibcallsInfo RTLCI(M.getTargetTriple());
22+
LLVMContext &Ctx = M.getContext();
23+
24+
for (RTLIB::LibcallImpl Impl : RTLCI.getLibcallImpls()) {
25+
if (Impl == RTLIB::Unsupported)
26+
continue;
27+
28+
// TODO: Declare with correct type, calling convention, and attributes.
29+
30+
FunctionType *FuncTy =
31+
FunctionType::get(Type::getVoidTy(Ctx), {}, /*IsVarArgs=*/true);
32+
33+
const char *FuncName = RTLCI.getLibcallImplName(Impl);
34+
M.getOrInsertFunction(FuncName, FuncTy);
35+
}
36+
37+
return PreservedAnalyses::none();
38+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; RUN: opt -S -passes=declare-runtime-libcalls -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s
2+
3+
; Check an already declared function
4+
; CHECK: declare float @logf(float)
5+
declare float @logf(float)
6+
7+
; Check an already defined function
8+
; CHECK: define float @sinf(float %x) {
9+
define float @sinf(float %x) {
10+
ret float %x
11+
}
12+
13+
; CHECK: declare void @acosf(...)
14+
; CHECK: declare void @__umodti3(...)
15+

llvm/utils/gn/secondary/llvm/lib/Transforms/Utils/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static_library("Utils") {
2929
"CtorUtils.cpp",
3030
"DXILUpgrade.cpp",
3131
"Debugify.cpp",
32+
"DeclareRuntimeLibcalls.cpp",
3233
"DemoteRegToStack.cpp",
3334
"EntryExitInstrumenter.cpp",
3435
"EscapeEnumerator.cpp",

0 commit comments

Comments
 (0)