-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP/POC: Constant Fold Logf128 calls
This is a proof of concept/work in progress patch. This patch enables ConstantFolding of log FP128 calls. This is achieved by querying with CMake if the host system has the logf128 symbol available. If so, replace the runtime call with the compile time constant returned from logf128. This approach could be considered controversial as cross-compiled llvm executables using shared objects may not have the logf128 symbol available at runtime. The implementation of logf128 may also yield different results on different targets, such as x86 using fp80 precision instead of the full fp128 range on other targets. This approach relies on unit tests, as more commonplace Clang/C tests and opt/llc/IR tests are not applicable since they are ignorant to the result of the compile time CMake check.
- Loading branch information
Showing
9 changed files
with
175 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//===- unittests/CodeGen/BufferSourceTest.cpp - MemoryBuffer source tests -===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Analysis/ConstantFolding.h" | ||
#include "llvm/Analysis/TargetLibraryInfo.h" | ||
#include "llvm/CodeGen/GlobalISel/CallLowering.h" | ||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/IR/InstrTypes.h" | ||
#include "gtest/gtest.h" | ||
|
||
using namespace llvm; | ||
|
||
namespace { | ||
|
||
class ConstantFoldLogf128Fixture | ||
: public ::testing ::TestWithParam<std::string> { | ||
protected: | ||
std::string FuncName; | ||
}; | ||
|
||
TEST_P(ConstantFoldLogf128Fixture, ConstantFoldLogf128) { | ||
LLVMContext Context; | ||
IRBuilder<> Builder(Context); | ||
Module MainModule("Logf128TestModule", Context); | ||
MainModule.setTargetTriple("aarch64-unknown-linux"); | ||
|
||
Type *FP128Ty = Type::getFP128Ty(Context); | ||
FunctionType *FP128Prototype = FunctionType::get(FP128Ty, false); | ||
Function *Logf128TestFunction = Function::Create( | ||
FP128Prototype, Function::ExternalLinkage, "logf128test", MainModule); | ||
BasicBlock *EntryBlock = | ||
BasicBlock::Create(Context, "entry", Logf128TestFunction); | ||
Builder.SetInsertPoint(EntryBlock); | ||
|
||
FunctionType *FP128FP128Prototype = | ||
FunctionType::get(FP128Ty, {FP128Ty}, false); | ||
Constant *Constant2L = ConstantFP::get128(FP128Ty, 2.0L); | ||
|
||
std::string FunctionName = GetParam(); | ||
Function *Logl = Function::Create( | ||
FP128FP128Prototype, Function::ExternalLinkage, FunctionName, MainModule); | ||
CallInst *LoglCall = Builder.CreateCall(Logl, Constant2L); | ||
|
||
TargetLibraryInfoImpl TLII(Triple(MainModule.getTargetTriple())); | ||
TargetLibraryInfo TLI(TLII, Logf128TestFunction); | ||
Constant *FoldResult = ConstantFoldCall(LoglCall, Logl, Constant2L, &TLI); | ||
|
||
#ifndef HAS_LOGF128 | ||
ASSERT_TRUE(FoldResult == nullptr); | ||
#else | ||
auto ConstantLog = dyn_cast<ConstantFP>(FoldResult); | ||
ASSERT_TRUE(ConstantLog); | ||
|
||
APFloat APF = ConstantLog->getValueAPF(); | ||
char LongDoubleHexString[0xFF]; | ||
unsigned Size = | ||
APF.convertToHexString(LongDoubleHexString, 32, true, | ||
APFloatBase::roundingMode::NearestTiesToAway); | ||
EXPECT_GT(Size, 0U); | ||
|
||
ASSERT_STREQ(LongDoubleHexString, | ||
std::string("0X1.62E42FEFA39E0000000000000000000P-1").c_str()); | ||
#endif | ||
} | ||
|
||
INSTANTIATE_TEST_SUITE_P(ConstantFoldLogf128, ConstantFoldLogf128Fixture, | ||
::testing::Values("logl", "llvm.log.f128")); | ||
|
||
} // end anonymous namespace |