Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

offsetof triggers LLVM assertion failure on StaticArray #13317

Closed
HertzDevil opened this issue Apr 14, 2023 · 0 comments · Fixed by #13319
Closed

offsetof triggers LLVM assertion failure on StaticArray #13317

HertzDevil opened this issue Apr 14, 2023 · 0 comments · Fixed by #13319
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:codegen topic:compiler:interpreter

Comments

@HertzDevil
Copy link
Contributor

The following triggers an LLVM assertion failure in LLVMOffsetOfElement:

offsetof(UInt8[4], @buffer)
.../include/llvm/Support/Casting.h:579: decltype(auto) llvm::cast(From*) [with To = StructType; From = Type]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' failed.

The relevant code is:

if type && (type.struct? || type.is_a?(TupleInstanceType))
offset = @program.offset_of(type.sizeof_type, ivar_index)

It breaks because the LLVM type for UInt8[4] is [4 x i8], which is an array type, not a struct type. (Crystal::Type#sizeof_type has no significant effect here.) LLVMOffsetOfElement only accepts struct types.

The same thing happens in the interpreter for pointerof, but via a different code path:

private def compile_pointerof_ivar(node : ASTNode, name : String)
closure_self = lookup_closured_var?("self")
if closure_self
get_closured_self_pointer(closure_self, name, node: node)
return
end
index = scope.index_of_instance_var(name).not_nil!
offset = if scope.struct?
@context.offset_of(scope, index)
else
@context.instance_offset_of(scope, index)
end
pointerof_ivar(offset, node: node)
end

The following is an empty prelude reproduction:

lib LibC
  fun exit(status : Int32) : NoReturn
end

def exit
  LibC.exit(0)
end

struct StaticArray(T, N)
  def to_unsafe
    pointerof(@buffer)
  end
end

x = uninitialized UInt8[4]
x.to_unsafe

Normal codegen doesn't have this issue with pointerof, as instance variable addresses there are always obtained at run time via getelementptr instructions instead, and no compile-time decision needs to be made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:compiler:codegen topic:compiler:interpreter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant