[cfe-dev] __builtin_shufflevector vs. llvm shufflevector

Eli Friedman eli.friedman at gmail.com
Wed Feb 8 12:08:05 PST 2012


On Wed, Feb 8, 2012 at 7:35 AM, Florian Pflug <fgp at phlo.org> wrote:
> Hi
>
> >From the llvm documentation I gather that llvm's shufflevector allows indices to be set to <undef> and interprets that as "don't care". From the initial discussion on clang's __builtin_shufflevector (http://lists.cs.uiuc.edu/pipermail/cfe-dev/2008-February/001151.html) it sounds as if the plan was to make "-1" translate to llvm's <undef>. This code fragment from clang's lib/CodeGen/CGExprScalar.cpp supports that theory
>
>  static llvm::Constant *getMaskElt(llvm::ShuffleVectorInst *SVI, unsigned Idx,
>                                    unsigned Off, llvm::Type *I32Ty) {
>    int MV = SVI->getMaskValue(Idx);
>    if (MV == -1)
>      return llvm::UndefValue::get(I32Ty);
>    return llvm::ConstantInt::get(I32Ty, Off+MV);
>  }
>
> clang 3.0, however, responds to
>
>  typedef signed char v2s8_t __attribute__ ((__ext_vector_type__ (2)));
>  v2s8_t v = (v2s8_t){(signed char)1, (signed char)2};
>  v = __builtin_shufflevector(v, v, 1, -1);
>
> with
>
>  error: index for __builtin_shufflevector must be less than the total number of vector elements
>
> Does that mean that the initial plan was abolished and masks with undef element aren't supported in clang, or is this simply a signed vs. unsigned bug in Sema::SemaBuiltinShuffleVector (as the phrasing of the error message makes me suspect)?

I'm pretty sure this is just an accident... I wasn't really thinking
about undef shuffle indexes when I wrote the code in SemaChecking.cpp.

> Also, are there plans to add support for zeroed elements to llvm's shufflevector? At least SSE's pshufb instruction supports that, but currently the only way to access that functionality is via __builtin__pshufb().

In theory, we should generate a pshufb if you shuffle a vector with
the zero vector and there isn't a more efficient choice.  It wouldn't
surprise me if we don't always pick the best lowering, though.

> best regards,
> Florian Pflug
>
> PS: Sorry if this is the wrong list to asks this question. I'd have posted on cfe-users, but such a thing does not seem to exist.

This list is fine.

-Eli




More information about the cfe-dev mailing list