Skip to content

Commit

Permalink
Enable assertion in C++ code in debug/force assertion build even if L…
Browse files Browse the repository at this point in the history
…LVM assertion is off.
  • Loading branch information
yuyichao committed Mar 6, 2017
1 parent b53d6f2 commit 8af48fc
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 21 deletions.
6 changes: 5 additions & 1 deletion Make.inc
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,13 @@ JULIACODEGEN := LLVM

# flag for disabling assertions
ifeq ($(FORCE_ASSERTIONS), 1)
# C++ code needs to include LLVM header with the same assertion flag as LLVM
# Use this flag to re-enable assertion in our code after all the LLVM headers are included
CXX_DISABLE_ASSERTION :=
DISABLE_ASSERTIONS :=
else
DISABLE_ASSERTIONS := -DNDEBUG
CXX_DISABLE_ASSERTION := -DJL_NDEBUG
DISABLE_ASSERTIONS := -DNDEBUG -DJL_NDEBUG
endif

# Compiler specific stuff
Expand Down
2 changes: 2 additions & 0 deletions src/APInt-C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <llvm/ADT/APFloat.h>
#include <llvm/Support/MathExtras.h>

#include "fix_llvm_assert.h"

#include "APInt-C.h"
#include "julia.h"

Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ $(BUILDDIR)/%.o: $(SRCDIR)/%.c $(HEADERS) | $(BUILDDIR)
$(BUILDDIR)/%.dbg.obj: $(SRCDIR)/%.c $(HEADERS) | $(BUILDDIR)
@$(call PRINT_CC, $(CC) $(CPPFLAGS) $(CFLAGS) $(DEBUGFLAGS) -c $< -o $@)
$(BUILDDIR)/%.o: $(SRCDIR)/%.cpp $(SRCDIR)/llvm-version.h $(HEADERS) $(LLVM_CONFIG_ABSOLUTE) | $(BUILDDIR)
@$(call PRINT_CC, $(CXX) $(shell $(LLVM_CONFIG_HOST) --cxxflags) $(CPPFLAGS) $(CXXFLAGS) $(SHIPFLAGS) -c $< -o $@)
@$(call PRINT_CC, $(CXX) $(shell $(LLVM_CONFIG_HOST) --cxxflags) $(CPPFLAGS) $(CXXFLAGS) $(SHIPFLAGS) $(CXX_DISABLE_ASSERTION) -c $< -o $@)
$(BUILDDIR)/%.dbg.obj: $(SRCDIR)/%.cpp $(SRCDIR)/llvm-version.h $(HEADERS) $(LLVM_CONFIG_ABSOLUTE) | $(BUILDDIR)
@$(call PRINT_CC, $(CXX) $(shell $(LLVM_CONFIG_HOST) --cxxflags) $(CPPFLAGS) $(CXXFLAGS) $(DEBUGFLAGS) -c $< -o $@)

Expand Down
2 changes: 1 addition & 1 deletion src/ccall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2039,7 +2039,7 @@ jl_cgval_t function_sig_t::emit_a_ccall(
assert(rtsz > 0);
Value *strct = emit_allocobj(ctx, rtsz, runtime_bt);
int boxalign = jl_gc_alignment(rtsz);
#ifndef NDEBUG
#ifndef JL_NDEBUG
#if JL_LLVM_VERSION >= 30600
const DataLayout &DL = jl_ExecutionEngine->getDataLayout();
#else
Expand Down
2 changes: 2 additions & 0 deletions src/cgmemmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

#ifdef USE_MCJIT
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include "fix_llvm_assert.h"
#include "julia.h"
#include "julia_internal.h"

#if JL_LLVM_VERSION >= 30700
#if JL_LLVM_VERSION < 30800
# include <llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h>
# include "fix_llvm_assert.h"
#endif
#ifdef _OS_LINUX_
# include <sys/syscall.h>
Expand Down
4 changes: 2 additions & 2 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ static Type *julia_struct_to_llvm(jl_value_t *jt, jl_unionall_t *ua, bool *isbox
jst->struct_decl = StructType::get(jl_LLVMContext, ArrayRef<Type*>(&latypes[0], ntypes));
}
}
#ifndef NDEBUG
#ifndef JL_NDEBUG
// If LLVM and Julia disagree about alignment, much trouble ensues, so check it!
if (jst->layout) {
const DataLayout &DL =
Expand Down Expand Up @@ -2001,7 +2001,7 @@ static void emit_unionmove(Value *dest, const jl_cgval_t &src, Value *skip, bool
else
copy_bytes = builder.CreateSelect(skip, ConstantInt::get(copy_bytes->getType(), 0), copy_bytes);
}
#ifndef NDEBUG
#ifndef JL_NDEBUG
// try to catch codegen errors early, before it uses this to memcpy over the entire stack
CreateConditionalAbort(builder, builder.CreateICmpEQ(copy_bytes, ConstantInt::get(T_int32, -1)));
#endif
Expand Down
5 changes: 3 additions & 2 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
#include <polly/RegisterPasses.h>
#include <polly/ScopDetection.h>
#endif
#include "fix_llvm_assert.h"

using namespace llvm;
namespace llvm {
Expand Down Expand Up @@ -827,7 +828,7 @@ static void CreateTrap(IRBuilder<> &builder)
builder.SetInsertPoint(newBB);
}

#ifndef NDEBUG
#ifndef JL_NDEBUG
static void CreateConditionalAbort(IRBuilder<> &builder, Value *test)
{
Function *f = builder.GetInsertBlock()->getParent();
Expand Down Expand Up @@ -5714,7 +5715,7 @@ static std::unique_ptr<Module> emit_function(
cur_prop.is_poploc = false;
jl_value_t *stmt = jl_array_ptr_ref(stmts, i);
jl_expr_t *expr = jl_is_expr(stmt) ? (jl_expr_t*)stmt : nullptr;
#ifndef NDEBUG
#ifndef JL_NDEBUG
if (jl_is_labelnode(stmt)) {
size_t lname = jl_labelnode_label(stmt);
if (lname != i + 1) {
Expand Down
6 changes: 2 additions & 4 deletions src/debuginfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@
#if JL_LLVM_VERSION >= 30700
# include <llvm/Object/ELFObjectFile.h>
#endif

#if defined(USE_MCJIT) && JL_LLVM_VERSION < 30600 && defined(_OS_DARWIN_)
#include "../deps/llvm-3.5.0/lib/ExecutionEngine/MCJIT/MCJIT.h"
#endif
#include "fix_llvm_assert.h"

using namespace llvm;

Expand Down Expand Up @@ -1040,6 +1037,7 @@ template<typename T>
static inline void ignoreError(T &err)
{
#if JL_LLVM_VERSION >= 30900 && !defined(NDEBUG)
// Needed only with LLVM assertion build
consumeError(err.takeError());
#endif
}
Expand Down
1 change: 1 addition & 0 deletions src/disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
#else
#include <llvm/DebugInfo.h>
#endif
#include "fix_llvm_assert.h"

#include "julia.h"
#include "julia_internal.h"
Expand Down
16 changes: 16 additions & 0 deletions src/fix_llvm_assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// This file is a part of Julia. License is MIT: http:https://julialang.org/license

// Include this file after every blocks of LLVM includes to set the assertion back.

#ifdef NDEBUG
# ifndef JL_NDEBUG
# undef NDEBUG
# include <assert.h>
// Set NDEBUG back so that we can include another LLVM header right after
# define NDEBUG
# endif
#else
# ifdef JL_NDEBUG
# undef JL_NDEBUG
# endif
#endif
3 changes: 2 additions & 1 deletion src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ namespace llvm {
#include <llvm/ADT/StringMap.h>
#include <llvm/ADT/StringSet.h>
#include <llvm/ADT/SmallSet.h>
#include "fix_llvm_assert.h"

using namespace llvm;

Expand Down Expand Up @@ -512,7 +513,7 @@ void *JuliaOJIT::getPointerToGlobalIfAvailable(const GlobalValue *GV)

void JuliaOJIT::addModule(std::unique_ptr<Module> M)
{
#ifndef NDEBUG
#ifndef JL_NDEBUG
// validate the relocations for M
for (Module::iterator I = M->begin(), E = M->end(); I != E; ) {
Function *F = &*I;
Expand Down
1 change: 1 addition & 0 deletions src/jitlayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extern PassManager *jl_globalPM;
#if JL_LLVM_VERSION >= 30500
#include <llvm/Target/TargetMachine.h>
#endif
#include "fix_llvm_assert.h"

extern "C" {
extern int globalUnique;
Expand Down
2 changes: 1 addition & 1 deletion src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ JL_DLLEXPORT int jl_type_morespecific(jl_value_t *a, jl_value_t *b);
jl_value_t *jl_unwrap_unionall(jl_value_t *v);
jl_value_t *jl_rewrap_unionall(jl_value_t *t, jl_value_t *u);

#ifdef NDEBUG
#if defined(NDEBUG) && defined(JL_NDEBUG)
STATIC_INLINE int jl_is_leaf_type_(jl_value_t *v)
{
return jl_is_datatype(v) && ((jl_datatype_t*)v)->isleaftype;
Expand Down
10 changes: 6 additions & 4 deletions src/llvm-gcroot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#endif
#include <llvm/IR/MDBuilder.h>

#include "fix_llvm_assert.h"

#include <vector>
#include <queue>
#include <set>
Expand All @@ -44,7 +46,7 @@ extern std::pair<MDNode*,MDNode*> tbaa_make_child(const char *name, MDNode *pare

namespace {

#ifndef NDEBUG
#ifndef JL_NDEBUG
static struct {
unsigned count;
unsigned locals;
Expand Down Expand Up @@ -778,7 +780,7 @@ void JuliaGCAllocator::allocate_frame()
if (CallInst *callInst = dyn_cast<CallInst>(user)) {
assert(bb == NULL);
bb = callInst->getParent();
#ifdef NDEBUG
#ifdef JL_NDEBUG
break;
#endif
}
Expand Down Expand Up @@ -1130,7 +1132,7 @@ void JuliaGCAllocator::allocate_frame()
}
}

#ifndef NDEBUG
#ifndef JL_NDEBUG
jl_gc_frame_stats.count++;
jl_gc_frame_stats.locals += argSpaceSize;
jl_gc_frame_stats.temp += maxDepth;
Expand Down Expand Up @@ -1232,7 +1234,7 @@ static RegisterPass<LowerGCFrame> X("LowerGCFrame", "Lower GCFrame Pass",
false /* Analysis Pass */);
}

#ifndef NDEBUG // llvm assertions build
#ifndef JL_NDEBUG // llvm assertions build
// gdb debugging code for inspecting the bb_uses map
void jl_dump_bb_uses(std::map<BasicBlock*, std::map<frame_register, liveness::id> > &bb_uses)
{
Expand Down
7 changes: 4 additions & 3 deletions src/llvm-ptls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
#endif
#include <llvm/IR/MDBuilder.h>

#include "julia.h"
#include "julia_internal.h"

#if JL_LLVM_VERSION >= 30700 && defined(JULIA_ENABLE_THREADING)
# include <llvm/IR/InlineAsm.h>
#endif
#include "fix_llvm_assert.h"

#include "julia.h"
#include "julia_internal.h"

using namespace llvm;

Expand Down
2 changes: 2 additions & 0 deletions src/llvm-simdloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Metadata.h>
#include <llvm/Support/Debug.h>
#include "fix_llvm_assert.h"

#include <cstdio>

namespace llvm {
Expand Down
1 change: 1 addition & 0 deletions src/llvm-version.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This file is a part of Julia. License is MIT: http:https://julialang.org/license

#include <llvm/Config/llvm-config.h>
#include "fix_llvm_assert.h"

#ifndef LLVM_VERSION_PATCH // for LLVM 3.3
#define LLVM_VERSION_PATCH 0
Expand Down
3 changes: 2 additions & 1 deletion src/runtime_ccall.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// This file is a part of Julia. License is MIT: http:https://julialang.org/license

#include "llvm-version.h"
#include <map>
#include <string>
#include <cstdio>
#include <llvm/Support/Host.h>
#include "fix_llvm_assert.h"
#include "julia.h"
#include "julia_internal.h"
#include "llvm-version.h"
using namespace llvm;

// --- library symbol lookup ---
Expand Down

0 comments on commit 8af48fc

Please sign in to comment.