[all-commits] [llvm/llvm-project] 5e999c: IR: Define byref parameter attribute

Matt Arsenault via All-commits all-commits at lists.llvm.org
Mon Jul 20 07:24:06 PDT 2020


  Branch: refs/heads/master
  Home:   https://github.com/llvm/llvm-project
  Commit: 5e999cbe8db0b50dc9828a1c062b4ffe84c5b137
      https://github.com/llvm/llvm-project/commit/5e999cbe8db0b50dc9828a1c062b4ffe84c5b137
  Author: Matt Arsenault <Matthew.Arsenault at amd.com>
  Date:   2020-07-20 (Mon, 20 Jul 2020)

  Changed paths:
    M llvm/docs/LangRef.rst
    M llvm/docs/ReleaseNotes.rst
    M llvm/include/llvm/Bitcode/LLVMBitCodes.h
    M llvm/include/llvm/IR/Argument.h
    M llvm/include/llvm/IR/Attributes.h
    M llvm/include/llvm/IR/Attributes.td
    M llvm/include/llvm/IR/Function.h
    M llvm/lib/Analysis/MemoryBuiltins.cpp
    M llvm/lib/AsmParser/LLLexer.cpp
    M llvm/lib/AsmParser/LLParser.cpp
    M llvm/lib/AsmParser/LLParser.h
    M llvm/lib/AsmParser/LLToken.h
    M llvm/lib/Bitcode/Reader/BitcodeReader.cpp
    M llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
    M llvm/lib/IR/AsmWriter.cpp
    M llvm/lib/IR/AttributeImpl.h
    M llvm/lib/IR/Attributes.cpp
    M llvm/lib/IR/Function.cpp
    M llvm/lib/IR/Verifier.cpp
    M llvm/lib/Transforms/Utils/CodeExtractor.cpp
    A llvm/test/Assembler/byref-parse-error-0.ll
    A llvm/test/Assembler/byref-parse-error-1.ll
    A llvm/test/Assembler/byref-parse-error-10.ll
    A llvm/test/Assembler/byref-parse-error-2.ll
    A llvm/test/Assembler/byref-parse-error-3.ll
    A llvm/test/Assembler/byref-parse-error-4.ll
    A llvm/test/Assembler/byref-parse-error-5.ll
    A llvm/test/Assembler/byref-parse-error-6.ll
    A llvm/test/Assembler/byref-parse-error-7.ll
    A llvm/test/Assembler/byref-parse-error-8.ll
    A llvm/test/Assembler/byref-parse-error-9.ll
    M llvm/test/Bitcode/attributes.ll
    A llvm/test/CodeGen/X86/byref.ll
    A llvm/test/Instrumentation/AddressSanitizer/byref-args.ll
    A llvm/test/Transforms/DeadArgElim/byref.ll
    A llvm/test/Transforms/Inline/byref-align.ll
    M llvm/test/Transforms/LowerConstantIntrinsics/objectsize_basic.ll
    A llvm/test/Verifier/byref.ll

  Log Message:
  -----------
  IR: Define byref parameter attribute

This allows tracking the in-memory type of a pointer argument to a
function for ABI purposes. This is essentially a stripped down version
of byval to remove some of the stack-copy implications in its
definition.

This includes the base IR changes, and some tests for places where it
should be treated similarly to byval. Codegen support will be in a
future patch.

My original attempt at solving some of these problems was to repurpose
byval with a different address space from the stack. However, it is
technically permitted for the callee to introduce a write to the
argument, although nothing does this in reality. There is also talk of
removing and replacing the byval attribute, so a new attribute would
need to take its place anyway.

This is intended avoid some optimization issues with the current
handling of aggregate arguments, as well as fixes inflexibilty in how
frontends can specify the kernel ABI. The most honest representation
of the amdgpu_kernel convention is to expose all kernel arguments as
loads from constant memory. Today, these are raw, SSA Argument values
and codegen is responsible for turning these into loads.

Background:

There currently isn't a satisfactory way to represent how arguments
for the amdgpu_kernel calling convention are passed. In reality,
arguments are passed in a single, flat, constant memory buffer
implicitly passed to the function. It is also illegal to call this
function in the IR, and this is only ever invoked by a driver of some
kind.

It does not make sense to have a stack passed parameter in this
context as is implied by byval. It is never valid to write to the
kernel arguments, as this would corrupt the inputs seen by other
dispatches of the kernel. These argumets are also not in the same
address space as the stack, so a copy is needed to an alloca. From a
source C-like language, the kernel parameters are invisible.
Semantically, a copy is always required from the constant argument
memory to a mutable variable.

The current clang calling convention lowering emits raw values,
including aggregates into the function argument list, since using
byval would not make sense. This has some unfortunate consequences for
the optimizer. In the aggregate case, we end up with an aggregate
store to alloca, which both SROA and instcombine turn into a store of
each aggregate field. The optimizer never pieces this back together to
see that this is really just a copy from constant memory, so we end up
stuck with expensive stack usage.

This also means the backend dictates the alignment of arguments, and
arbitrarily picks the LLVM IR ABI type alignment. By allowing an
explicit alignment, frontends can make better decisions. For example,
there's real no advantage to an aligment higher than 4, so a frontend
could choose to compact the argument layout. Similarly, there is a
high penalty to using an alignment lower than 4, so a frontend could
opt into more padding for small arguments.

Another design consideration is when it is appropriate to expose the
fact that these arguments are all really passed in adjacent
memory. Currently we have a late IR optimization pass in codegen to
rewrite the kernel argument values into explicit loads to enable
vectorization. In most programs, unrelated argument loads can be
merged together. However, exposing this property directly from the
frontend has some disadvantages. We still need a way to track the
original argument sizes and alignments to report to the driver. I find
using some side-channel, metadata mechanism to track this
unappealing. If the kernel arguments were exposed as a single buffer
to begin with, alias analysis would be unaware that the padding bits
betewen arguments are meaningless. Another family of problems is there
are still some gaps in replacing all of the available parameter
attributes with metadata equivalents once lowered to loads.

The immediate plan is to start using this new attribute to handle all
aggregate argumets for kernels. Long term, it makes sense to migrate
all kernel arguments, including scalars, to be passed indirectly in
the same manner.

Additional context is in D79744.




More information about the All-commits mailing list