Skip to content

[SILOptimizer] Add ‘llvm.cttz.*’ Intrinsic support to Constant Folding #23720

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

Merged
merged 1 commit into from
Apr 2, 2019

Conversation

shajrawi
Copy link

@shajrawi shajrawi commented Apr 1, 2019

rdar://problem/29522851

@shajrawi shajrawi requested a review from gottesmm April 1, 2019 23:11
@shajrawi
Copy link
Author

shajrawi commented Apr 1, 2019

@swift-ci Please test and merge

@swift-ci swift-ci merged commit f8e4c75 into swiftlang:master Apr 2, 2019
@shajrawi shajrawi deleted the cttz branch April 2, 2019 05:54
// Undefined
return nullptr;
}
LZ = LHSI.getBitWidth();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do an early exit here?

Copy link
Contributor

@gottesmm gottesmm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post commit review. Please fix.

}
LZ = LHSI.getBitWidth();
} else {
switch (ID) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion: Use an inline constructor:

unsigned LZ = [&] {
...
}();

Also. Do you really think we are going to support more types of intrinsics here? Why not just put in an assert or something like that.

@@ -278,6 +278,44 @@ constantFoldBinaryWithOverflow(BuiltinInst *BI, BuiltinValueKind ID,
ResultsInError);
}

static SILValue countZeros(BuiltinInst *BI, llvm::Intrinsic::ID ID) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this name means nothing and this needs a comment. My suggestion:

/// Constant fold a cttz or ctlz builtin inst of an integer literal. If \p countLeadingZeros is set to true, then we
/// assume \p bi must be ctlz. If false, \p bi must be cttz.
///
/// NOTE: We assert that \p bi is either cttz or ctlz.
static SILValue constantFoldCountLeadingOrTrialingZeroIntrinsic(BuiltinInst *bi, bool countLeadingZeros) {
   ...
}

/// Count either the leading or trailing zeros.

Also, do you really need the intrinsic id here? Couldn't you just pass in a bool stating which of the two you are handling?

@@ -278,6 +278,44 @@ constantFoldBinaryWithOverflow(BuiltinInst *BI, BuiltinValueKind ID,
ResultsInError);
}

static SILValue countZeros(BuiltinInst *BI, llvm::Intrinsic::ID ID) {
assert(BI->getArguments().size() == 2 && "Ctlz should have 2 args.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bad assert message. You are handling both ctlz and cttz.

@gottesmm
Copy link
Contributor

gottesmm commented Apr 2, 2019

Another thing. You can make all of this much simpler by using PatternMatch.h.

@shajrawi
Copy link
Author

shajrawi commented Apr 2, 2019

Thanks @gottesmm - #23739

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants