Skip to content

[globalopt] optimization leaves store to a constant global #42961

Description

@aqjune
Bugzilla Link 43616
Version trunk
OS All
Attachments input
CC @aqjune,@LebedevRI,@preames,@nunoplopes

Extended Description

$ opt -version
LLVM (http://llvm.org/):
  LLVM version 10.0.0svn
  Optimized build with assertions.
  Default target: x86_64-apple-darwin19.0.0
  Host CPU: skylake

$ cat input.ll
declare {}* @​llvm.invariant.start.p0i8(i64 %size, i8* nocapture %ptr)

define void @​test1(i8* %ptr) {
  call {}* @​llvm.invariant.start.p0i8(i64 4, i8* %ptr)
  ret void
}

@​object1 = global i32 0
define void @​ctor1() {
  store i32 -1, i32* @​object1
  %A = bitcast i32* @​object1 to i8*
  call void @​test1(i8* %A)
  ret void
}

@​llvm.global_ctors = appending constant
  [1 x { i32, void ()*, i8* }]
  [ { i32, void ()*, i8* } { i32 65535, void ()* @​ctor1, i8* null } ]

$ opt -globalopt -S -o - input.ll
@​object1 = constant i32 -1
@​llvm.global_ctors = appending constant [0 x { i32, void ()*, i8* }] zeroinitializer

; Function Attrs: argmemonly nounwind willreturn
declare {}* @​llvm.invariant.start.p0i8(i64 immarg, i8* nocapture) #​0

define void @​test1(i8* %ptr) local_unnamed_addr {
  %1 = call {}* @​llvm.invariant.start.p0i8(i64 4, i8* %ptr)
  ret void
}

define void @​ctor1() local_unnamed_addr {
  store i32 -1, i32* @​object1
  %A = bitcast i32* @​object1 to i8*
  call void @​test1(i8* %A)
  ret void
}

attributes #​0 = { argmemonly nounwind willreturn }

Running -globalopt leaves a store to @​object1, which is a constant global, which may cause crash because LangRef states that it can be placed in the read-only section of an executable.

LangRef says:
A variable may be defined as a global constant, which indicates that the contents of the variable will never be modified (enabling better optimization, allowing the global data to be placed in the read-only section of an executable, etc).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillaipoInterprocedural optimizationsmiscompilation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions