This is an archive of the discontinued LLVM Phabricator instance.

[OpenCL] Disable vector to scalar types coercion for OpenCL
Needs ReviewPublic

Authored by cdai2 on Jan 30 2023, 6:44 PM.

Details

Summary

For x86 target, vector types (both result and arguments) can be coerced
to scalars of the same size, e.g:

define zeroext i1 @_Z18convert_ulong4_rteDv4_t(<4 x i16> %x)
; becomes
define zeroext i1 @_Z18convert_ulong4_rteDv4_t(i64 %x.coerced)

Such behavior is completely valid for x86, but the backend vectorizer
cannot work with scalars instead of vectors.

With this patch, argument and result types will be leaved unchanged in
the CodeGen.

New option fopencl-force-vector-abi is also added to force-disables
vector to scalar coercion when provided.

Diff Detail

Event Timeline

cdai2 created this revision.Jan 30 2023, 6:44 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 30 2023, 6:44 PM
cdai2 requested review of this revision.Jan 30 2023, 6:44 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 30 2023, 6:44 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
cdai2 updated this revision to Diff 493470.Jan 30 2023, 6:54 PM

Forget code format

It doesn't seem to make sense to have a LangOpt for this, if anything a new CodeGenOpt feels like a better fit. Then you should probably extend existing classify functions to prevent coercion instead of adding yet another function otherwise you will need to handle all the different corner cases again as there seem to be quite many if I look at X86_32ABIInfo::classifyReturnType...

FYI OpenCL vectors are implemented as generic vector extension so this probably shouldn't be bind to OpenCL at all:
https://clang.llvm.org/docs/LanguageExtensions.html#vectors-and-extended-vectors?

Perhaps it can be a general feature to prevent coercion? But then other targets have similar logic too... should it then be more generalized?

Such behavior is completely valid for x86, but the backend vectorizer
cannot work with scalars instead of vectors.

With this patch, argument and result types will be leaved unchanged in
the CodeGen.

New option fopencl-force-vector-abi is also added to force-disables
vector to scalar coercion when provided.

Do you have any public examples to look at? The coercion is part of the ABI so it should ideally be respected by other tools.

Does the x86 backend lower these small vectors to the same ABI? If so, I think we could just teach Clang unconditionally that it doesn't need this coercion on x86 targets. If not, this is an ABI break; are you sure that's okay?