Emitting loop metadata in codegen


As discussed earlier I'm implementing #pragma ivdep in clang. I use AttributedStmt with an IVDepAttr to wrap loop Stmts in the AST. So far I have only hacked in codegen to attach loop metadata.

To do this correctly I need to attach metadata to all predecessors of the loop header. Also, I need to attach metadata to all loads and stores in the loop. (see llvm.loop.parallel and llvm.mem.parallel_loop_access)

I'm wondering what the best way to implement this in clang codegen? I think this could get really messy and become very fragile if I just start modifying EmitForStmt, etc to check for some flag.

One idea I've been thinking about is creating the metadata after code emission using a llvm loop pass (living in clang/lib/CodeGen) Basically during codegen I would attach a simple metadata (clang.pragma.ivdep or something) to a block in the loop and use the pass to translate to the metadata understood by llvm. I don't see a precedent for running clang-specific passes in codegen though..

Comments and suggestions are appreciated.


Dear Paul and others,

   I would like to ask whether you made any progress with this,
we are actually trying to do a similar thing:

- One thing is to introduce ICC-like pragmas and add support for them in the vectorizer
   (in fact this seems like the thing you were doing).
- Second one is to identify a block of code that should be identified as an
   instruction set extension (ISE) and automatically generate a
   description for an ADL Codal used in Codasip Framework.

I successfully added some pragmas to the clang, but currently I am trying to figure out how to
correctly store metadata.

Do you have some clues i could follow? E.g. if the code is not private to Intel, could I take a look?:slight_smile:

Thank you and best regards


    I am writing here a solution to the pragmas and annotating code:

    I found Paul's patch at


and it provides a nice guidelines for adding new pragmas with metadata.
However metadatas have
I then handled code annotation with this code in CGStmt.cpp

void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) {

   bool generateAnnotation = false;

   typedef ArrayRef<const Attr*> AttrList;
   AttrList Attrs = S.getAttrs();
   for (AttrList::iterator I = Attrs.begin(), IE = Attrs.end(); I != IE; ++I) {
     if (isa<MyAttr>(*I)) {
       generateAnnotation = true;

   if (generateAnnotation)
    llvm::Value *Args[4] = {
      Builder.CreateBitCast(CGM.EmitAnnotationString("str1"), Int8PtrTy),
      Builder.CreateBitCast(CGM.EmitAnnotationString("pragma_begin"), Int8PtrTy),
      Builder.CreateBitCast(CGM.EmitAnnotationUnit(S.getLocStart()), Int8PtrTy),
    llvm::Value *V = Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation), Args);



This the generates annotation intrinsics that are kept by opt at the original place
(unlike metadata).

call void @llvm.var.annotation(...

I hope these annotations won't make any optimizations weaker, but in the LLVM IR documetation is written that they are
ignored by opt.

Best regards