[cfe-dev] pass-by-value aggregates - using the callee's frame

Robert Lytton robert at xmos.com
Wed Jul 3 11:28:13 PDT 2013


Hi David,

That is very helpful and clear.
I'll put my hack to one side and look at the llvm library again.
Thank you for your answer. Much appreciate your time!

robert
________________________________________
From: David Blaikie [dblaikie at gmail.com]
Sent: 03 July 2013 17:51
To: Robert Lytton
Cc: cfe-dev Developers
Subject: Re: [cfe-dev] pass-by-value aggregates - using the callee's frame

On Wed, Jul 3, 2013 at 8:52 AM, Robert Lytton <robert at xmos.com> wrote:
> Hi David,
>
> yes, simple bit copyable structures.
> I am new to llvm and clang so appologise for asking the obvious.
>
> If I run using the -emit-llvm, I can clearly see the 'byval' attributes -
> all correct and sensible.
>
> By using -emit-llvm am I seeinf what the clang front end is creating to be
> passed to the llvm target backend?

there's some terminology issues here (to Clang hackers, anything
beyond Clang (ie: all of LLVM) is sometimes "the backend" - but to
LLVM hackers, only the actual target-specific stuff (not the IR-to-IR
(so called "mid level") optimizations) is the 'backend'), but you're
more or less on the right track. -emit-llvm (at -O0) is basically just
what Clang generated.

> OR is this an intermediate IR within clang stages?

Nope - Clang has an AST as an intermediate, Clang's "CodeGen" (better
referred to as "IRGen" - and will be renamed as such one day)
generates LLVM IR - at which point it's handed off to LLVM & Clang
doesn't have much to do with it anymore.

> may be I am over simplifying where clang fits in (e.g. front and back).

Right - there's a 'middle', which is still part of LLVM (called from
Clang, but not part of the Clang codebase) - the IR-to-IR
optimizations.

> From my fiddling it seems the '-emit-llvm' is then passed through the
> CodeGen stage in clang, and particularly the
> CodeGenFunction::EmitFunctionProlog() function.
> I'm left pondering what the relationship between the llvm codegen and the
> clang codegen is?

Clang CodeGen (really IRGen) produces LLVM IR from Clang ASTs.
LLVM CodeGen (really CodeGen) produces machine code (or assembly, etc)
from LLVM IR.

Source -> AST -> IR -> better IR -> ASM/machine code

> - not taken a close look yet - Tomorrow!
>
>
> I hacked the EmitFunctionProlog() to see if I could get what I needed - and
> I can (hack below).
> but am I on the right track or way off?

I'm not especially familiar with this - my only suggestion, as before,
is that you should just need to cause Clang's CodeGen (really IRGen)
to produce the 'byval' attribute on the right parameters you want this
to happen on. You shouldn't need to manually emit new/different IR
instructions, I don't think.

That's just my rough guess.

> I took a look in the target backend briefly and will look again.
> (Also tried -debug to dump the stages)
>
> robert
>
> --- clang/lib/CodeGen/CGCall.cpp
> +++ clang/lib/CodeGen/CGCall.cpp
> @@ -1290,6 +1290,14 @@ void CodeGenFunction::EmitFunctionProlog(const
> CGFunctionInfo &FI,
>                                 false);
>            V = AlignedTemp;
>          }
> +        else if (ArgI.getIndirectByVal()) {
> +          llvm::AllocaInst *ByValue
> +                              = CreateMemTemp(Ty, V->getName() +
> "agg.tmp");
> +          llvm::ConstantInt * size = llvm::ConstantInt::get(IntPtrTy,
> +                        getContext().getTypeSizeInChars(Ty).getQuantity());
> +          Builder.CreateMemCpy(ByValue, V, size, 4 ); // where do we get
> alignment from?
> +          V = ByValue;
> +        }
>        } else {
>          // Load scalar value from indirect argument.
>          CharUnits Alignment = getContext().getTypeAlignInChars(Ty);
>
>
> ________________________________
> From: David Blaikie [dblaikie at gmail.com]
> Sent: 03 July 2013 15:51
> To: Robert Lytton
> Cc: cfe-dev Developers
> Subject: Re: [cfe-dev] pass-by-value aggregates - using the callee's frame
>
> I believe (assuming you're talking about bit-copiable aggregates (not C++
> types with non-trivial copy)) this is handled in Clang's irgen by using the
> 'byval' llvm ir attribute. I don't recall the specifics, but if shouldn't be
> too hard to find some handling of that in there.
>
> On Jul 3, 2013 12:44 AM, "Robert Lytton" <robert at xmos.com> wrote:
>>
>> Hi,
>>
>> I'm adding an XCore target front end.
>> The XCore ABI is the same as the DefaultABIInfo ... almost.
>>
>> One outstanding issue is the need to copy pass-by-value aggregates into
>> the callee's stack-frame during the prolog.
>> Any advise, suggestions or direction as to where to look
>> (CodeGenFunction::EmitFunctionProlog()? ) most welcome.
>>
>> robert
>>
>>
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>
>




More information about the cfe-dev mailing list