Skip to content

Constants #54

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

Conversation

CodaFi
Copy link
Member

@CodaFi CodaFi commented Jan 26, 2017

Introduces a new typesafe API for integral constants. The intent is to make it as natural as possible to interact with constants, so they come equipped with a DSL that allows Swift's native arithmetic operators to be applied in place of LLVM constant expression calls. Tests are included to make sure const-operand-having instructions really are const-folded away.

@CodaFi CodaFi force-pushed the constantly-constituted-constrainted-constructions branch 2 times, most recently from 4f4a066 to bbe4b40 Compare January 26, 2017 23:42
@CodaFi CodaFi changed the title [WIP] Draft a constants API Constants Jan 27, 2017
@CodaFi CodaFi requested a review from harlanhaskins January 27, 2017 01:41
@CodaFi
Copy link
Member Author

CodaFi commented Jan 27, 2017

Done. @harlanhaskins?

Copy link
Member

@harlanhaskins harlanhaskins left a comment

Choose a reason for hiding this comment

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

I love it!

}

public static func +(lhs: Constant, rhs: Constant) -> Constant {
precondition(lhs.repr == rhs.repr, "Mixed-representation constant operations are disallowed")
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we could bubble this up to type-level? Make Representation an internal protocol with three members and make Constant generic. Then + wouldn't apply between Constant<Unsigned> and Constant<Floating>

Copy link
Member Author

@CodaFi CodaFi Jan 27, 2017

Choose a reason for hiding this comment

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

All these asserts are sanity checks. All operations are homogenous, the type is generic.

/// behavior of the resulting constant value.
///
/// - returns: A constant value representing the negation of the given constant.
public static func neg(_ lhs: Constant<Signed>, overflowBehavior: OverflowBehavior = .default) -> Constant<Signed> {
Copy link
Member Author

Choose a reason for hiding this comment

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

Should these be spelled out?

Copy link
Member

Choose a reason for hiding this comment

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

I think negate should

Copy link
Member Author

Choose a reason for hiding this comment

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

multiply and subtract are out?

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, back to my computer. I'm actually not sure why the spelled-out versions need to exist. But I think maybe they should be fully-spelled-out.

Copy link
Member Author

Choose a reason for hiding this comment

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

They need to exist because overflowBehavior.

Copy link
Member

Choose a reason for hiding this comment

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

Right! Then I think they should be spelled out. 👍

@CodaFi CodaFi force-pushed the constantly-constituted-constrainted-constructions branch from c3d8c85 to fa9c151 Compare January 27, 2017 04:20
@CodaFi
Copy link
Member Author

CodaFi commented Jan 27, 2017

⛵️

@CodaFi CodaFi merged commit 9b55780 into llvm-swift:master Jan 27, 2017
@CodaFi CodaFi deleted the constantly-constituted-constrainted-constructions branch January 27, 2017 05:07
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.

2 participants