【问题标题】:llvm ir optimisation "Use still stuck around after Def is destroyed" error function deletionllvm ir优化“Def被破坏后使用仍然卡住”错误函数删除
【发布时间】:2018-05-16 09:07:38
【问题描述】:

您好,我最近升级到 llvm 4.0,现在我从优化器那里收到了最奇怪的错误,似乎是最良性的代码。我已经确定了似乎给我以下错误的代码,以及 llvm 给我的与<badref> 相关的断言的打印结果,但是在它所谈论的代码中没有看到<badref>。这让我觉得可能是编译器错误或生成的代码不正确。

; ModuleID = '__form__module.$static.196'
source_filename = "__form__module.$static.196"
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"

%struct.clousure_type = type { void ()*, %struct.closure_vtable*, i8* }
%struct.closure_vtable = type { %struct.type_info*, void (i8*, i8*)*, void (i8*)*, i8 (i8*, i8*)*, i64 (i8*, i8*)* }
%struct.type_info = type { i8*, i8*, i64, i8, i8, i8, i8, %struct.type_info**, %struct.member_info* }
%struct.member_info = type { i8, %struct.type_info* }

@"$static.159$stub_ptr" = external hidden externally_initialized global void (%struct.clousure_type*, %struct.clousure_type*)*

define hidden void @"$static.196"(%struct.clousure_type*, %struct.clousure_type*, %struct.clousure_type*, i8) {
entry:
  %4 = icmp eq i8 %3, 0
  br i1 %4, label %bb2, label %bb4

bb2:                                              ; preds = %entry
  %5 = icmp eq %struct.clousure_type* %0, %2
  br i1 %5, label %bb3, label %bb6

bb3:                                              ; preds = %bb4, %bb2
  ret void

bb4:                                              ; preds = %entry
  %6 = icmp eq %struct.clousure_type* %1, %2
  br i1 %6, label %bb3, label %bb6

bb6:                                              ; preds = %bb4, %bb2
  tail call void @"$static.159"(%struct.clousure_type* %0, %struct.clousure_type* %1)
  ret void
}

; Function Attrs: alwaysinline
define available_externally hidden void @"$static.159"(%struct.clousure_type*, %struct.clousure_type*) #0 {
entry:
  %2 = load void (%struct.clousure_type*, %struct.clousure_type*)*, void (%struct.clousure_type*, %struct.clousure_type*)** @"$static.159$stub_ptr", align 8
  tail call void %2(%struct.clousure_type* %0, %struct.clousure_type* %1)
  ret void
}

attributes #0 = { alwaysinline }


While deleting: void (%struct.clousure_type*, %struct.clousure_type*)* %$static.159
Use still stuck around after Def is destroyed:  call void @"$static.159"(%struct.clousure_type* <badref>, %struct.clousure_type* %0)

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
Assertion failed!

Program: C:\Users\RICH\workspace\formvm\Debug\formvm.exe
File: C:\llvm-4.0.0.src\lib\IR\Value.cpp, Line 85

Expression: use_empty() && "Uses remain when a value is destroyed!"

优化器正在处理的 llvm ir 代码,代表上述 2 个函数,是以下 2 个函数(由 c++ 生成)。

define private void @gen__fun__return_copy85(%struct.clousure_type*, %struct.clousure_type*, %struct.clousure_type*, i8) {
entry:
  %4 = alloca %struct.clousure_type*, align 8
  store %struct.clousure_type* %0, %struct.clousure_type** %4, align 8
  %5 = load %struct.clousure_type*, %struct.clousure_type** %4, align 8
  %6 = alloca %struct.clousure_type*, align 8
  store %struct.clousure_type* %1, %struct.clousure_type** %6, align 8
  %7 = load %struct.clousure_type*, %struct.clousure_type** %6, align 8
  %8 = alloca %struct.clousure_type*, align 8
  store %struct.clousure_type* %2, %struct.clousure_type** %8, align 8
  %9 = load %struct.clousure_type*, %struct.clousure_type** %8, align 8
  %10 = alloca i8, align 8
  store i8 %3, i8* %10, align 8
  %11 = load i8, i8* %10, align 8
  %12 = icmp eq i8 %11, 0
  br i1 %12, label %bb2, label %bb4

bb2:                                              ; preds = %entry
  %13 = icmp eq %struct.clousure_type* %5, %9
  br i1 %13, label %bb3, label %bb6

bb3:                                              ; preds = %bb2
  ret void

bb4:                                              ; preds = %entry
  %14 = icmp eq %struct.clousure_type* %7, %9
  br i1 %14, label %bb5, label %bb6

bb5:                                              ; preds = %bb4
  ret void

bb6:                                              ; preds = %bb4, %bb2
  call void @_ZN13clousure_typeC2ERKS_(%struct.clousure_type* %5, %struct.clousure_type* %7)
  ret void
}

define private void @_ZN13clousure_typeC2ERKS_(%struct.clousure_type* %this, %struct.clousure_type* %rhs) {
  %1 = alloca %struct.clousure_type*, align 8
  %2 = alloca %struct.clousure_type*, align 8
  store %struct.clousure_type* %this, %struct.clousure_type** %1, align 8
  store %struct.clousure_type* %rhs, %struct.clousure_type** %2, align 8
  %3 = load %struct.clousure_type*, %struct.clousure_type** %1, align 8
  %4 = getelementptr %struct.clousure_type, %struct.clousure_type* %3, i32 0, i32 0
  store void ()* null, void ()** %4, align 8
  %5 = getelementptr %struct.clousure_type, %struct.clousure_type* %3, i32 0, i32 1
  store %struct.closure_vtable* null, %struct.closure_vtable** %5, align 8
  %6 = getelementptr %struct.clousure_type, %struct.clousure_type* %3, i32 0, i32 2
  store i8* null, i8** %6, align 8
  %7 = load %struct.clousure_type*, %struct.clousure_type** %2, align 8
  call void @_ZN13clousure_type4copyERKS_(%struct.clousure_type* %3, %struct.clousure_type* %7)
  ret void
}

要添加以下通行证,我是否需要更改任何添加任何内容,其中哪些可能会导致问题?

    FPM->add(llvm::createCFGSimplificationPass());
    FPM->add(llvm::createInstructionCombiningPass());
    FPM->add(llvm::createPromoteMemoryToRegisterPass());
    FPM->add(llvm::createCFGSimplificationPass());
    FPM->add(llvm::createLICMPass());
    FPM->add(llvm::createLoopDeletionPass());
    FPM->add(llvm::createLoopUnrollPass());
    FPM->add(llvm::createSCCPPass());
    FPM->add(llvm::createCFGSimplificationPass());
    FPM->add(llvm::createInstructionCombiningPass());
    FPM->add(llvm::createDeadStoreEliminationPass());
    FPM->add(llvm::createAggressiveDCEPass());
    FPM->add(llvm::createCFGSimplificationPass());
    FPM->add(llvm::createTailCallEliminationPass());

即使我删除了所有通行证,我仍然会收到错误消息!

目前看来问题源于以下函数@"$static.159",而不是该函数尝试load @"$static.159$stub_ptr"。理论是它在尝试加载自身时无法删除?

; Function Attrs: alwaysinline
define available_externally hidden void @"$static.159"(%struct.clousure_type*, %struct.clousure_type*) #0 {
entry:
  %2 = load void (%struct.clousure_type*, %struct.clousure_type*)*, void (%struct.clousure_type*, %struct.clousure_type*)** @"$static.159$stub_ptr"
  tail call void %2(%struct.clousure_type* %0, %struct.clousure_type* %1)
  ret void
}

【问题讨论】:

    标签: compilation llvm compiler-optimization jit llvm-ir


    【解决方案1】:

    需要升级到 llvm 5.0 才能最终解决此错误。

    这不是理论上的问题。简单地说,我存储在函数指针中的一个函数,由于生成上述代码的 c++ 代码中的一个不正确初始化的指针导致了上面找到的&lt;badref&gt;,因此没有进行分配。注意&lt;badref&gt;是后来由于使用了函数指针才被优化器发现的!!

    为什么这个指针之前没有导致段错误我不知道但它没有导致错误的代码生成。在 llvm 5.0 上,指针按预期给出了段错误,使我能够找到错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 1970-01-01
      • 2013-07-28
      • 1970-01-01
      • 2014-10-30
      相关资源
      最近更新 更多