Said R. Adda b. Ahabah: We circumcise him in either case: if he is viable, he is rightly circumcised; if not, one [merely] cuts flesh (which is permissible on Shabbat).
However, this does get to the issue of a non-viable infant. According to tradition any child who lives more than 30 days is considered “viable.” Mourning customs are not done for one who dies earlier. And yet:
A child was born to the son of R. Dimi b. Joseph, [and] it died within thirty days. [Thereupon] he sat and mourned for it.
The same happened with R. Kahana. Both are visited by others who tell them the law and reprimand them for mourning. They both come up with answers for why they are mourning anyway.
The law may be what it is – but it hard to interfere with human emotion. And that is a good thing.