-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang] Implement __builtin_{clzg,ctzg} #83431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3128,36 +3128,66 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, | |
case Builtin::BI__builtin_ctzs: | ||
case Builtin::BI__builtin_ctz: | ||
case Builtin::BI__builtin_ctzl: | ||
case Builtin::BI__builtin_ctzll: { | ||
Value *ArgValue = EmitCheckedArgForBuiltin(E->getArg(0), BCK_CTZPassedZero); | ||
case Builtin::BI__builtin_ctzll: | ||
case Builtin::BI__builtin_ctzg: { | ||
bool HasFallback = BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_ctzg && | ||
E->getNumArgs() > 1; | ||
|
||
Value *ArgValue = | ||
HasFallback ? EmitScalarExpr(E->getArg(0)) | ||
: EmitCheckedArgForBuiltin(E->getArg(0), BCK_CTZPassedZero); | ||
|
||
llvm::Type *ArgType = ArgValue->getType(); | ||
Function *F = CGM.getIntrinsic(Intrinsic::cttz, ArgType); | ||
|
||
llvm::Type *ResultType = ConvertType(E->getType()); | ||
Value *ZeroUndef = Builder.getInt1(getTarget().isCLZForZeroUndef()); | ||
Value *ZeroUndef = | ||
Builder.getInt1(HasFallback || getTarget().isCLZForZeroUndef()); | ||
Value *Result = Builder.CreateCall(F, {ArgValue, ZeroUndef}); | ||
if (Result->getType() != ResultType) | ||
Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, | ||
"cast"); | ||
return RValue::get(Result); | ||
if (!HasFallback) | ||
return RValue::get(Result); | ||
|
||
Value *Zero = Constant::getNullValue(ArgType); | ||
Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero"); | ||
Value *FallbackValue = EmitScalarExpr(E->getArg(1)); | ||
Value *ResultOrFallback = | ||
Builder.CreateSelect(IsZero, FallbackValue, Result, "ctzg"); | ||
return RValue::get(ResultOrFallback); | ||
} | ||
case Builtin::BI__builtin_clzs: | ||
case Builtin::BI__builtin_clz: | ||
case Builtin::BI__builtin_clzl: | ||
case Builtin::BI__builtin_clzll: { | ||
Value *ArgValue = EmitCheckedArgForBuiltin(E->getArg(0), BCK_CLZPassedZero); | ||
case Builtin::BI__builtin_clzll: | ||
case Builtin::BI__builtin_clzg: { | ||
bool HasFallback = BuiltinIDIfNoAsmLabel == Builtin::BI__builtin_clzg && | ||
E->getNumArgs() > 1; | ||
|
||
Value *ArgValue = | ||
HasFallback ? EmitScalarExpr(E->getArg(0)) | ||
: EmitCheckedArgForBuiltin(E->getArg(0), BCK_CLZPassedZero); | ||
|
||
llvm::Type *ArgType = ArgValue->getType(); | ||
Function *F = CGM.getIntrinsic(Intrinsic::ctlz, ArgType); | ||
|
||
llvm::Type *ResultType = ConvertType(E->getType()); | ||
Value *ZeroUndef = Builder.getInt1(getTarget().isCLZForZeroUndef()); | ||
Value *ZeroUndef = | ||
Builder.getInt1(HasFallback || getTarget().isCLZForZeroUndef()); | ||
Value *Result = Builder.CreateCall(F, {ArgValue, ZeroUndef}); | ||
if (Result->getType() != ResultType) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if the way this is handling isCLZForZeroUndef() is what you want? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since in the additions below I check if the argument itself is zero instead of checking the result of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was more thinking of the opposite: we don't need to make the result of the clz defined if we're not using it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. Is it worth making tests for this with a target where |
||
Result = Builder.CreateIntCast(Result, ResultType, /*isSigned*/true, | ||
"cast"); | ||
return RValue::get(Result); | ||
if (!HasFallback) | ||
return RValue::get(Result); | ||
|
||
Value *Zero = Constant::getNullValue(ArgType); | ||
Value *IsZero = Builder.CreateICmpEQ(ArgValue, Zero, "iszero"); | ||
Value *FallbackValue = EmitScalarExpr(E->getArg(1)); | ||
Value *ResultOrFallback = | ||
Builder.CreateSelect(IsZero, FallbackValue, Result, "clzg"); | ||
return RValue::get(ResultOrFallback); | ||
} | ||
case Builtin::BI__builtin_ffs: | ||
case Builtin::BI__builtin_ffsl: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should
Constexpr
be in this list of attributes?https://godbolt.org/z/Efd3EW5xE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will implement
constexpr
support. It requires adding a few lines in clang/lib/AST/ExprConstant.cpp and clang/lib/AST/Interp/InterpBuiltin.cpp.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #86549