Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions llvm/include/llvm/CodeGen/LinkAllAsmWriterComponents.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@
#define LLVM_CODEGEN_LINKALLASMWRITERCOMPONENTS_H

#include "llvm/IR/BuiltinGCs.h"
#include <cstdlib>
#include "llvm/Support/AlwaysTrue.h"

namespace {
struct ForceAsmWriterLinking {
ForceAsmWriterLinking() {
// We must reference the plug-ins in such a way that compilers will not
// delete it all as dead code, even with whole program optimization,
// yet is effectively a NO-OP. As the compiler isn't smart enough
// to know that getenv() never returns -1, this will do the job.
// This is so that globals in the translation units where these functions
// are defined are forced to be initialized, populating various
// registries.
if (std::getenv("bar") != (char*) -1)
// yet is effectively a NO-OP. This is so that globals in the translation
// units where these functions are defined are forced to be initialized,
// populating various registries.
if (llvm::getNonFoldableAlwaysTrue())
return;

llvm::linkOcamlGCPrinter();
Expand Down
12 changes: 5 additions & 7 deletions llvm/include/llvm/CodeGen/LinkAllCodegenComponents.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@

#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/SchedulerRegistry.h"
#include "llvm/Support/AlwaysTrue.h"
#include "llvm/Target/TargetMachine.h"
#include <cstdlib>

namespace {
struct ForceCodegenLinking {
ForceCodegenLinking() {
// We must reference the passes in such a way that compilers will not
// delete it all as dead code, even with whole program optimization,
// yet is effectively a NO-OP. As the compiler isn't smart enough
// to know that getenv() never returns -1, this will do the job.
// This is so that globals in the translation units where these functions
// are defined are forced to be initialized, populating various
// registries.
if (std::getenv("bar") != (char*) -1)
// yet is effectively a NO-OP. This is so that globals in the translation
// units where these functions are defined are forced to be initialized,
// populating various registries.
if (llvm::getNonFoldableAlwaysTrue())
return;

(void) llvm::createFastRegisterAllocator();
Expand Down
14 changes: 6 additions & 8 deletions llvm/include/llvm/ExecutionEngine/MCJIT.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@
#define LLVM_EXECUTIONENGINE_MCJIT_H

#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/Support/AlwaysTrue.h"
#include "llvm/Support/Compiler.h"
#include <cstdlib>

extern "C" LLVM_ABI void LLVMLinkInMCJIT();

namespace {
struct ForceMCJITLinking {
ForceMCJITLinking() {
// We must reference MCJIT in such a way that compilers will not
// delete it all as dead code, even with whole program optimization,
// yet is effectively a NO-OP. As the compiler isn't smart enough
// to know that getenv() never returns -1, this will do the job.
// This is so that globals in the translation units where these functions
// are defined are forced to be initialized, populating various
// registries.
if (std::getenv("bar") != (char*) -1)
// delete it all as dead code, even with whole program optimization, yet
// is effectively a NO-OP. This is so that globals in the translation
// units where these functions are defined are forced to be initialized,
// populating various registries.
if (llvm::getNonFoldableAlwaysTrue())
return;

LLVMLinkInMCJIT();
Expand Down
8 changes: 3 additions & 5 deletions llvm/include/llvm/LinkAllIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/AlwaysTrue.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Memory.h"
Expand All @@ -29,19 +30,16 @@
#include "llvm/Support/Process.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/Signals.h"
#include <cstdlib>

namespace {
struct ForceVMCoreLinking {
ForceVMCoreLinking() {
// We must reference VMCore in such a way that compilers will not
// delete it all as dead code, even with whole program optimization,
// yet is effectively a NO-OP. As the compiler isn't smart enough
// to know that getenv() never returns -1, this will do the job.
// delete it all as dead code, even with whole program optimization.
// This is so that globals in the translation units where these functions
// are defined are forced to be initialized, populating various
// registries.
if (std::getenv("bar") != (char*) -1)
if (llvm::getNonFoldableAlwaysTrue())
return;
llvm::LLVMContext Context;
(void)new llvm::Module("", Context);
Expand Down
15 changes: 7 additions & 8 deletions llvm/include/llvm/LinkAllPasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "llvm/CodeGen/Passes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRPrintingPasses.h"
#include "llvm/Support/AlwaysTrue.h"
#include "llvm/Support/Valgrind.h"
#include "llvm/Transforms/IPO.h"
#include "llvm/Transforms/IPO/AlwaysInliner.h"
Expand All @@ -54,14 +55,12 @@ class Triple;
namespace {
struct ForcePassLinking {
ForcePassLinking() {
// We must reference the passes in such a way that compilers will not
// delete it all as dead code, even with whole program optimization,
// yet is effectively a NO-OP. As the compiler isn't smart enough
// to know that getenv() never returns -1, this will do the job.
// This is so that globals in the translation units where these functions
// are defined are forced to be initialized, populating various
// registries.
if (std::getenv("bar") != (char *)-1)
// We must reference the passes in such a way that compilers will not delete
// it all as dead code, even with whole program optimization, yet is
// effectively a NO-OP. This is so that globals in the translation units
// where these functions are defined are forced to be initialized,
// populating various registries.
if (llvm::getNonFoldableAlwaysTrue())
return;

(void)llvm::createAtomicExpandLegacyPass();
Expand Down
25 changes: 25 additions & 0 deletions llvm/include/llvm/Support/AlwaysTrue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===--- AlwaysTrue.h - Helper for oqaque truthy values --*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_ALWAYS_TRUE_H
#define LLVM_SUPPORT_ALWAYS_TRUE_H

#include <cstdlib>

namespace llvm {
inline bool getNonFoldableAlwaysTrue() {
// Some parts of the codebase require a "constant true value" used as a
// predicate. These cases require that even with LTO and static linking,
// it's not possible for the compiler to fold the value. As compilers
// aren't smart enough to know that getenv() never returns -1, this will do
// the job.
return std::getenv("LLVM_IGNORED_ENV_VAR") != (char *)-1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding a dependency on getenv, did you consider using an empty side-effecting inline asm? THat would produce lower code size and reduce an unnecessary libc dependency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did, but decided to keep it as-is.

  • MSVC for x86-64 doesn't support inline assembly
  • getenv is already a dependency

}
} // end namespace llvm

#endif // LLVM_SUPPORT_ALWAYS_TRUE_H
3 changes: 2 additions & 1 deletion llvm/tools/bugpoint/bugpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "llvm/LinkAllIR.h"
#include "llvm/LinkAllPasses.h"
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Support/AlwaysTrue.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/PluginLoader.h"
Expand Down Expand Up @@ -111,7 +112,7 @@ int main(int argc, char **argv) {
initializeInstCombine(Registry);
initializeTarget(Registry);

if (std::getenv("bar") == (char*) -1) {
if (!llvm::getNonFoldableAlwaysTrue()) {
InitializeAllTargets();
InitializeAllTargetMCs();
InitializeAllAsmPrinters();
Expand Down
11 changes: 5 additions & 6 deletions polly/include/polly/LinkAllPasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "polly/Support/DumpFunctionPass.h"
#include "polly/Support/DumpModulePass.h"
#include "llvm/ADT/StringRef.h"
#include <cstdlib>
#include "llvm/Support/AlwaysTrue.h"

namespace llvm {
class Pass;
Expand Down Expand Up @@ -72,11 +72,10 @@ extern char &CodePreparationID;
namespace {
struct PollyForcePassLinking {
PollyForcePassLinking() {
// We must reference the passes in such a way that compilers will not
// delete it all as dead code, even with whole program optimization,
// yet is effectively a NO-OP. As the compiler isn't smart enough
// to know that getenv() never returns -1, this will do the job.
if (std::getenv("bar") != (char *)-1)
// We must reference the passes in such a way that compilers will not delete
// it all as dead code, even with whole program optimization, yet is
// effectively a NO-OP.
if (llvm::getNonFoldableAlwaysTrue())
return;

polly::createCodePreparationPass();
Expand Down